对set集合的每个单词遍历不同位置切分,查询左右两部分是否在集合中
不要直接枚举左右两部分判断复合词是否在集合中时间复杂度较大
#include<cstdio>
#include<iostream>
#include<set>
#include<cstring>
using namespace std;
set<string> words;
int main(){
string word,left,right;
while(cin>>word){
words.insert(word);
}
for(set<string>::iterator it=words.begin();it!=words.end();++it){
word=*it;
for(int j=1;j<word.size();j++){
left.assign(word,0,j);
if(words.count(left)){
right.assign(word,j,word.size()-j);
if(words.count(right)){
cout<<word<<endl;;
break;
}
}
}
}
return 0;
}