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
题解:看是否有一个单词是由其他两个单词构成的。用string.substr(a,b)//始a,终b 来拆分string,再来寻找。
#include <iostream>
#include <set>
#include <cstdio>
#include <string>
using namespace std;
set <string> v;//set容器
int main()
{
string st;
set <string>::iterator p;//迭代器
while(cin>>st)
v.insert(st);
for(p=v.begin(); p!=v.end(); p++)
{
st=*p;
for(int i=0; i<st.length()-1; i++)
{
string sub1=st.substr(0,i+1);//拆分string,再去寻找
string sub2=st.substr(i+1,st.length()-(i+1));
if( v.find(sub1)!=v.end() && v.find(sub2 )!=v.end() )//查找
{
printf("%s\n",st.c_str());//将string用%s输出
break;
}
}
}
return 0;
}
uva :
原题链接
国内链接:https://vjudge.net/contest/169852#problem/E