Word Amalgamation

链接

[https://vjudge.net/contest/212939#problem/C]

题意

给你个字典,字典包含若干个单词;
再给你若干个单词,让你输出跟这个单词有相同的字母的字典里的单词(不考虑顺序)

分析

STL set的应用以及next_permutation的应用
用set保存字典里的单词,对于每个要查的单词s先按字典序排列,然后next_permutation生成s的全排列
再用set.find(s)找是否存在这种排列,有输出。没有输出“no”;

代码

#include<iostream>
#include<set>
#include<algorithm>
#include<string>
using namespace std;
int main(){
	set<string> se;
	set<string>::iterator it;
	string s;
	int cnt=0;
	//freopen("in.txt","r",stdin);
	while(cin>>s){
		if(s=="XXXXXX") {
			cnt++;
			continue;
		}
		if(cnt==0)
		se.insert(s);
		if(cnt==2) break;
		if(cnt==1){
			bool flag=0;
			sort(s.begin(),s.end());
			do{
				it=se.find(s);
				if(it!=se.end()){
					cout<<s<<endl;
					flag=1;
				}
			}while(next_permutation(s.begin(),s.end()));
			if(!flag) cout<<"NOT A VALID WORD\n";
			cout<<"******\n";
		}
	}
	return 0;
}
posted @ 2018-10-29 21:15  ChunhaoMo  阅读(321)  评论(0)    收藏  举报