HDU_1113Word Amalgamation

来源:互联网 发布:花生壳linux版客户端 编辑:程序博客网 时间:2024/06/04 17:48
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 <iostream>    #include <string>    #include <map>    #include <algorithm>    using namespace std;    map<string, string> str;    string s, t;    int main()    {        while(cin >> s && s != "XXXXXX")        {            t = s;            sort(s.begin(), s.end());            str[t] = s;        }        while(cin >> s && s != "XXXXXX")        {            bool flag = 0;            t = s;            sort(s.begin(), s.end());            for(map<string, string>::iterator it=str.begin(); it!=str.end(); ++it)                if(it->second == s)                {                    cout << it->first << endl;                    flag = 1;                }                if(flag == 0)                    cout << "NOT A VALID WORD\n";                cout << "******\n";        }        return 0;    }
思路解析:

学习了一天的map,至今感觉意犹未尽。。。我的VC编译器下架了,Code:: Blocks上市了。呵呵只想说好强大。。。

参考这道题,只是学习了一些皮毛而已

第一个XXXXXX前面的是给出的字典,之后的是查询的关键词语,目标是找到字典中与其有相同的字母所构成的字符

串。两者字符长度一样,参考输出,多个满足时按字典序输出所有满足的。

0 0