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
tarpgivenscorerefundonlytrapworkearncoursepepperpartXXXXXXresconfudreaptrsettoresucXXXXXX
Sample Output
score******refund******parttarptrap******NOT A VALID WORD******course******
Source
>>
问题链接:。
问题描述:参见上文。
问题分析:这是一个字典问题,自然用map来实现。问题在于还需要转个弯,不然会掉进陷阱里去的。
查字典问题,通常是单词按照字典顺序存放,然后将要查的单词拆成字母,按单词的字母顺序去查字典。然而要是这样做程序的逻辑就太零碎繁杂了。
于是,把单词中的字符排个顺序作为关键字,来查单词的话就方便了。需要注意的一点,不同的单词有可能具有相同的关键字。
程序说明:这个程序的关键有容器类map的使用,算法库<algorithm>中函数sort()的使用。
这个程序未必是最佳做法,使用map<string vector<string>>作为字典也许是最佳的做法。
AC的C++语言程序如下:
/* HDU1113 POJ1318 UVA642 ZOJ1181 UVALive5328 Word Amalgamation */#include#include