入门经典-习题5-5,41-uva10391复合词-string assign,set⭐⭐⭐⭐⭐难度:1

对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;
}

猜你喜欢

转载自blog.csdn.net/qq_41093189/article/details/79596686