入門經典-習題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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章