2021-03-18

D. Word Amalgamation

Description
In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. Your task is to write a program that can unscramble words.

Input
The input contains four parts: 1) a dictionary, which consists of at least one and at most 100 words, one per line; 2) a line containing XXXXXX, which signals the end of the dictionary; 3) one or more scrambled ‘words’ that you must unscramble, each on a line by itself; and 4) another line containing XXXXXX, which signals the end of the file. All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X’s.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique.

Output
For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line “NOT A VALID WORD” instead. In either case, output a line containing six asterisks to signal the end of the list.

题目大意
本题大意是,给一个字典,再给出几个单词查询,若该单词可以通过各种排列组合后得到一个字典里的单词,那么就输出这个字典里的单词。本题可以统计各个查询单词的各个字母的数量,再与字典里的单词一一对比;
AC代码


string lw(string &s) {
	int i;
	string ss;
	ss=s;
	sort(ss.begin(),ss.end());
	return ss;
}

int main() {
	string s,s2,s3,s4;
	map<string,int> m;
	set<string> words;
	int i;
	m.clear();
	while (cin>>s) {
		if (s=="XXXXXX") break;
		//s2=lw(s);
		words.insert(s);
		//m[lw(s)]++;
	}
	while(cin>>s) {
		if(s=="XXXXXX") return 0;
		int f=0;
		s2=lw(s);
		set<string>::iterator it;//set迭代器: 读入单词的同时将单词加入set,因为set自身的排序功能,所以输出时只需遍历一遍set,如果set当前单词在文本中仅有一个,则输出这个单词。
		for (it=words.begin(); it!=words.end(); it++) {
			s3=*it;
			s4=lw(s3);
			if (s4==s2) {
				cout<<*it<<endl;
				f=1;
			}
		}
		if(f==0) cout<<"NOT A VALID WORD"<<"\n";
		cout<<"******"<<"\n";
	}

	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值