UVa 10391 Compound Words(set)

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

题意

按字典序给你单词,找出所有单词是仅有2个其他单词组合起来的

题解

用set存所有字符串,再把字符串分割成两部分,用count查找(logn),由于是集合set,所以找到返回1,未找到返回0

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     //freopen("in.txt","r",stdin);
 6     //freopen("out.txt","w",stdout);
 7     set<string> se;
 8     string str;
 9     while(cin>>str)
10         se.insert(str);
11     for(auto str:se)
12     {
13         int Len=str.size();
14         for(int i=1;i<Len-1;i++)
15         {
16             if(se.count(str.substr(0,i))&&se.count(str.substr(i)))
17             {
18                 cout<<str<<endl;
19                 break;
20             }
21         }
22     }
23     return 0;
24 }
原文地址:https://www.cnblogs.com/taozi1115402474/p/8453420.html