HDOJ 1113

来源:互联网 发布:中航王岚财富知多少 编辑:程序博客网 时间:2024/06/06 08:58

Word Amalgamation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2666    Accepted Submission(s): 1283


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******
题目的大致意思是,输入有四个部分,第一个部分是有序的单词,第二部分是“XXXXXX”,代表第一部分的输入结束,第三部分是无序的单词,第四部分是“XXXXXX”,代表第四部分的输入结束。题目要输出的是每一个无序的单词对应的有序单词,且是按字典序排列。思路是首先对整个有序部分进行单词按字典序上升,然后有序部分以及无序的部分进行单词内部sort,比如tarp->aprt,这样无序部分的单词就和有序部分的单词就可以直接判断是否匹配。最后挨个匹配输出就好了。
代码如下:
#include <iostream>#include <string.h>#include <cstring>#include <string>#include <algorithm>#include <stack>#include <queue>#include <stdio.h>#include <stdlib.h>#include <cstdlib>#include <cmath>#include <math.h>#include <vector>#include <map>#include <time.h>#include <ctime>using namespace std;#define M 150vector <string> s1;vector <string> s2;vector <string> s3;vector <string> s4;int cmp(char a, char b);int cmp1(string a, string b);int main(){    string a;    while(cin >> a)    {        if(a == "XXXXXX")            break;        s1.clear();        s2.clear();        s3.clear();        s4.clear();        s1.push_back(a);        while(cin >> a)        {            if(a == "XXXXXX")                break;            s1.push_back(a);        }        while(cin >> a)        {            if(a == "XXXXXX")                break;            s2.push_back(a);        }        sort(s1.begin(),s1.end(),cmp1);        for(int i = 0; i < s1.size(); i++)        {            s3.push_back(s1[i]);            sort(s3[i].begin(),s3[i].end(),cmp);        }        for(int i = 0; i < s2.size(); i++)        {            s4.push_back(s2[i]);            sort(s4[i].begin(),s4[i].end(),cmp);        }        int flag1 = 0;        for(int i = 0; i < s2.size(); i++)        {            flag1 = 0;            for(int j = 0; j < s1.size(); j++)            {                if(s4[i] == s3[j])                {                    flag1 = 1;                    cout << s1[j] << endl;                }                if(j == s1.size() - 1)                {                    if(flag1 == 0)                        cout << "NOT A VALID WORD" << endl;                    cout  << "******" <<endl;                }            }        }    }    return 0;}int cmp(char a, char b){    return a < b;}int cmp1(string a, string b){    return a < b;}

0 0
原创粉丝点击