Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9395 | Accepted: 4498 |
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 ******
Source
Regionals 1998 >> North
America - Mid-Central USA
问题链接:HDU1113 POJ1318 UVA642 ZOJ1181 UVALive5328 Word Amalgamation。
问题描述:参见上文。
问题分析:这是一个字典问题,自然用map来实现。问题在于还需要转个弯,不然会掉进陷阱里去的。
查字典问题,通常是单词按照字典顺序存放,然后将要查的单词拆成字母,按单词的字母顺序去查字典。然而要是这样做程序的逻辑就太零碎繁杂了。
于是,把单词中的字符排个顺序作为关键字,来查单词的话就方便了。需要注意的一点,不同的单词有可能具有相同的关键字。
程序说明:这个程序的关键有容器类map的使用,算法库<algorithm>中函数sort()的使用。
这个程序未必是最佳做法,使用map<string vector<string>>作为字典也许是最佳的做法。
AC的C++语言程序如下:
/* HDU1113 POJ1318 UVA642 ZOJ1181 UVALive5328 Word Amalgamation */ #include <iostream> #include <map> #include <string> #include <algorithm> using namespace std; int main() { map<string, string> dic; string word, key; while(cin >> word && word != "XXXXXX") { key = word; sort(key.begin(), key.end()); dic[word] = key; } while(cin >> key && key != "XXXXXX") { sort(key.begin(), key.end()); bool flag = true; for(map<string, string>::iterator it=dic.begin(); it!=dic.end(); it++) { if(it->second == key) { cout << it->first << endl; flag = false; } } if(flag) cout << "NOT A VALID WORD" << endl; cout << "******" << endl; } return 0; }