题目描述
给出一个词典,找出所有的复合词,即恰好有两个单词连接而成的单词。输入每行都是一个小写字母组成的单词。输入已按照字典序从小到大排序,且不超过120000个单词。输出所有复合词,按照字典序从小到大排序。
输入输出样例
输入
a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra
输出
alien
newborn
#include<iostream>
#include<cstring>
#include<string>
#include<set>
using namespace std;
set<string>st;
int main()
{
for(string s;cin >> s;)
{
st.insert(s);
}
for(set<string>::iterator it = st.begin();it != st.end();++it)
{
for(int j = 1;j < (int)it->size();++j)
{
if(st.count(it->substr(0,j)) && st.count(it->substr(j,it->size()-j)))
{
cout<<*it<<endl;
break;
}
}
}
return 0;
}