You are to findall the two-word compound words in a dictionary. A two-word compound word is aword in the dictionary that is the concatenation of exactly two other words inthe dictionary.
Input
Standard inputconsists of a number of lowercase words, one per line, in alphabetical order.There will be no more than 120,000 words.
Output
Your output shouldcontain all the compound words, one per line, in alphabetical order.
Sample Input
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra
Sample Output
alien
newborn
代码:
#include<iostream>
#include<string>
#include<set>
using namespacestd;
set<string>dict;
int main()
{
string s;
while(cin>>s)
{
dict.insert(s);
}
s="";
for(set<string>::iteratorit=dict.begin(); it!=dict.end(); it++)
{
s=*it;
for(int i=1; i<s.length(); i++)
{
if(dict.find(s.substr(0,i))!=dict.end()&&dict.find(s.substr(i,s.length()-i))!=dict.end())
{
cout<<s<<endl;
break;
}
}
}
return 0;
}