码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-1113(map的运用)

时间:2015-08-10 19:26:45      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

Word Amalgamation

Problem 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.
 
 
Sample Input
tarp
given
score
refund
only
trap
work
earn
course
pepper
part
XXXXXX
resco
nfudre
aptr
sett
oresuc
XXXXXX
 

 

Sample Output
score
******
refund
******
part
tarp
trap
******
NOT A VALID WORD
******
course
******
 
分析:题意是给你一个单词字典,每次询问一串字符,问能否组成字典里的单词,并按字典序输出。这题开始尝试暴力查找,但没注意到要按字典序,后来尝试用set,但第三个样例出错了,怎么也想不通,最后换成map,把字典里的每个单词里的字符排序并与原单词对应,然后查找时排个序再比较就很方便了。
最后说一句,map大法好~~~
 
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <set>
 8 #include <vector>
 9 #include <sstream>
10 #include <queue>
11 #include <typeinfo>
12 #include <fstream>
13 #include <map>
14 #include <stack>
15 using namespace std;
16 #define INF -100000
17 typedef long long ll;
18 const int maxn=110;
19 string words,tmp,query;
20 map<string,string> ans;
21 int main()
22 {
23 //    freopen("in.txt","r",stdin);
24 //    freopen("out.txt","w",stdout);
25     while(cin>>words){
26         if(words=="XXXXXX") break;
27         tmp=words;
28         sort(words.begin(),words.end());
29         ans[tmp]=words;
30     }
31     while(cin>>query){
32         if(query=="XXXXXX") break;
33         int flag=0;
34         sort(query.begin(),query.end());
35         for(map<string,string>::iterator it=ans.begin();it!=ans.end();it++)
36             if(it->second==query){
37                 cout<<it->first<<endl;
38                 flag=1;
39             }
40         if(!flag) printf("NOT A VALID WORD\n");
41         printf("******\n");
42     }
43     return 0;
44 }

 

HDU-1113(map的运用)

标签:

原文地址:http://www.cnblogs.com/RRirring/p/4718701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!