首页 > 代码库 > UVA - 10391

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

aalienbornlesslienneverneverthelessnewnewbornthezebra

 

Sample Output

aliennewborn

思路很简单,将一个单词拆成好两个单词,然后搜索是否存在这两个单词就可以,注意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

 

UVA - 10391