UVA - 10391

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

思路很简单,将一个单词拆成好两个单词,然后搜索是否存在这两个单词就可以,注意break;

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <map>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 map<string,int> IDcatch; 
10 vector<string> v;
11 int main () {
12     string str;
13     while (cin >> str) {
14         v.push_back(str);
15         IDcatch[str] = 1;
16     }
17     for (int i = 0;i < v.size();i++) {
18         for (int j = 0,len = v[i].length();j < len;j++) {
19             if (IDcatch.count(v[i].substr(0,j)) && IDcatch.count(v[i].substr(j,len - j))) {
20                 cout << v[i] << endl;
21                 break;
22             }
23         }
24     }
25 }
View Code

 

posted @ 2014-11-18 18:34  闪光阳  阅读(163)  评论(0编辑  收藏  举报