ZOJ1825 Compound Words(set)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/han_hhh/article/details/81157013

Compound Words


Time Limit: 5 Seconds      Memory Limit: 32768 KB


You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.


Input

Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 120,000 words.


Output

Your output should contain all the compound words, one per line, in alphabetical order.


Sample Input

a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra


Sample Output

alien
newborn


Source: University of Waterloo Local Contest 1996.09.28

先将单词放入字符串集合中,再对于所有单词s,将其分为所有可能的字符串t1,t2,在字符串集合中找是否有t1,t2

扫描二维码关注公众号,回复: 3047848 查看本文章

 要会使用函数substr(),会方便很多


#include<iostream>
#include<string>
#include<set>
using namespace std;
int main(void){
    string s,t1,t2;
    int i,len;
    set<string> sset;
    while(cin>>s)
        sset.insert(s);
    for(set<string>::iterator it=sset.begin();it!=sset.end();it++){
        s=*it;
        len=s.length();
        for(i=0;i<len-1;i++){
            t1=s.substr(0,i+1);
            t2=s.substr(i+1,len-1);
            if(sset.find(t1)!=sset.end()&&sset.find(t2)!=sset.end()){
                cout<<s<<endl;
                break;
            }
        }

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/han_hhh/article/details/81157013