Compound Words

来源:互联网 发布:农业数据库 编辑:程序博客网 时间:2024/06/08 02:05
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

#include <iostream>#include <cstdio>#include <cstring>#include <set>using namespace std;set <string> a;int main(){    string s,s1,s2;    set <string>::iterator it;    while(cin>>s)        a.insert(s);    for(it=a.begin();it!=a.end();it++){        s=*it;        for(int i=0;i<s.length()-1;i++)        {            s1=s.substr(0,i+1);            s2=s.substr(i+1,s.length()-(i+1));            if(a.find(s1)!=a.end()&&a.find(s2)!=a.end())            {            cout<<s.c_str()<<endl;                break;            }        }    }    return 0;}


原创粉丝点击