Word Amalgamation hdu1113

来源:互联网 发布:网络家装公司 编辑:程序博客网 时间:2024/06/01 11:36

Word Amalgamation

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 4
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
tarpgivenscorerefundonlytrapworkearncoursepepperpartXXXXXXresconfudreaptrsettoresucXXXXXX
 

Sample Output
score******refund******parttarptrap******NOT A VALID WORD******course******
字符串问题,本题的大意就是在多个单词中,查找输入的混乱的单词,并查找在原来的字典中是否存在这个单词,如果存在多个就按字典序输出
代码如下:
#include <cstdio>#include <iostream>#include <algorithm>#include <map>#include <cstring>#include <queue>using namespace std;#define maxn 120#define maxx 10map<string,priority_queue<string,vector<string>,greater<string> > > mapp;<span style="white-space:pre"></span>//map存储输入的字典,key为排序之后的单词,value为原始的单词,并在存储value的时候用priority_queue进行对多个单词排序。int main(){char a[maxx],b[maxx];map<string,priority_queue<string,vector<string>,greater<string> > >::iterator iter;<span style="white-space:pre"></span>//定义迭代器。while(1)<span style="white-space:pre"></span>//输入字典{scanf("%s",a);if(strcmp(a,"XXXXXX")==0)break;strcpy(b,a);sort(a,a+strlen(a));mapp[a].push(b);}while(1)<span style="white-space:pre"></span>//对测试实例处理并查找输出。{scanf("%s",a);if(strcmp(a,"XXXXXX")==0)break;sort(a,a+strlen(a));iter = mapp.find(a);<span style="white-space:pre"></span>//查找单词a所在的位置,并返回迭代器iter。if(iter==mapp.end())printf("NOT A VALID WORD\n");else{int n=iter->second.size();for(int i=0;i<n;i++)<span style="white-space:pre"></span>//输出优先队列中的单词。{cout<<iter->second.top()<<endl;iter->second.pop();}}cout<<"******"<<endl;}return 0;}



0 0
原创粉丝点击