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
题意:
找出该单词是由给出的单词组成;
并按照字典的顺序输出;
<span style="font-size:18px;">#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
set <string> Set;
int main () {
string str;
while (cin >> str) {
Set.insert(str);
}
set<string>::iterator it;
for (it = Set.begin(); it != Set.end(); it++) {
for (int i = 1; i < (*it).length(); i++) {
string front = (* it).substr(0,i); // 找出从0-i的字符串
string rear = (* it).substr(i,(*it).length()); // 从i到最后
if (Set.count(front) && Set.count(rear)) {
cout << (*it) <<endl;
break;
}
}
}
return 0;
}</span>