首页 > 代码库 > 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 }
UVA - 10391
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。