您的位置:首页 > 其它

uva 10391 Compound Words <set>

2016-05-10 12:58 531 查看
Compound Words

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 theconcatenation 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, inalphabetical 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;

set<string>dic;
set<string>::iterator t;

int main()
{
string word;
while(cin >> word)dic.insert(word);

for(t = dic.begin(); t != dic.end(); t++){
word = *t;
int n = word.length();

for(int i = 0; i < n - 1; i++ ){
string s1=word.substr(0,i+1),s2 = word.substr(i+1,n-i-1);
if( (dic.count(s1)) && (dic.count(s2)) ){
cout << word <<endl;
break;
}
}
}
//system("pause");
return 0;
}


dic.count(s2) 在set dic中计算s2的个数,并返回其个数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: