码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 10391

时间:2014-11-18 23:33:53      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

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;

bubuko.com,布布扣
 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

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/xiaoshanshan/p/4106486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!