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
题意很简单就是你在上面的那些单词当中查询,是否存在合成词,并且合成词的两个单词也必须在字母表当中。
这个我学会一个新的STL的函数,那就拆分函数substr,天下合久必分,分久必合。
#include<iostream>
#include<map>
#include<string>
using namespace std;
string s[1201000];
int main()
{
int i=0,j,k;
map<string,int>b;
b.clear();
while(cin>>s[i])
{
b[s[i]]=1;
i++;
}
string c,d;
for(j=0; j<i; j++)
{
for(k=0; k<s[j].length()-1; k++)
{
//if(b[s[j]]!=1)
// continue;
c=s[j].substr(0,k+1);
if(b[c]!=1)
continue;
d=s[j].substr(k+1);
if(b[d]==1)
{
cout<<s[j]<<endl;
break;
}
else
continue;
}
}
return 0;
}