题目:
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
题目大意:
给出一些单词,要求找出那些由两个其它单词组成的单词。
解题思路:
用set存储单词,暴力判断每一个单词是否是组合单词。
代码:
#include <iostream>
#include <set>
#include <string>using namespace std;
int main(){
set<string> ss;
string str;
while(cin>>str){
//if(str=="0")break;
ss.insert(str);
}
set<string>::iterator ite;
for(ite=ss.begin();ite!=ss.end();ite++){
int le=(*ite).length();
string cur=*ite;
for(int i=1;i<le;i++){
string left=cur.substr(0,i);
string right=cur.substr(i);
if(ss.count(left)&&ss.count(right)){
cout<<cur<<endl;
break;
}
}
}
return 0;
}