2012-06-08 14:13:07Morris
[UVA][set使用] 10391 - Compound Words
Problem E: 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 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 <stdio.h>
#include <iostream>
#include <set>
using namespace std;
int main() {
string line, s1, s2;
set<string> S;
while(cin >> line)
S.insert(line);
for(set<string>::iterator i = S.begin(); i != S.end(); i++) {
int len = (*i).length();
for(int j = 0; j < len-1; j++) {
s1 = (*i).substr(0, j+1);
s2 = (*i).substr(j+1, len-j);
if(S.find(s1) != S.end() && S.find(s2) != S.end()) {
cout << (*i) << endl;
break;
}
}
}
return 0;
}