Word Amalgamation之 multimap的应用

来源:互联网 发布:网络小票机ip设置 编辑:程序博客网 时间:2024/05/21 16:23

Word Amalgamation

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


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******
 

Source
Mid-Central USA 1998
要注意对得到的单词进行排序,否则会WA的。
#include<iostream>#include<cstdio>#include<string>#include<algorithm>#include<queue>#include<vector>#include<map>using namespace std;multimap<char *, char *> m;multimap<char *, char *>::iterator k, jend;bool cmp(char a, char b){    return a-'a'< b-'a';}bool cmp2(char *a,char *b){return strcmp(a,b);}void init(){    bool flag;    int i, j;    char c;    while (true){        i = 0;        char *a = new char[20];        char *b = new char[20];        flag = false;        while ((c = getchar()) != '\n'){            a[i] = c;            b[i] = c;            i++;        }        if (i == 6)        for (j = 0; j < i; j++){            if (a[j] != 'X'){                flag = true;                break;            }        }        else            flag = true;        if (!flag)            break;        a[i] = '\0';        b[i] = '\0';        sort(b, b + i, cmp);        m.insert(pair<char *, char *>(b, a));    }}int main(){        char a[20];char d[101][20];    char c;    int i,j;int t;    bool flag;        freopen("in.txt","r",stdin);    init();               jend = m.end();        i = 0;        while (true){            i = 0; t=0;            flag = false;            while ((c = getchar()) != '\n'){                a[i] = c;                i++;            }            a[i] = '\0';            if (i == 6)            for (j = 0; j < i; j++){                if (a[j] != 'X'){                    flag = true;                    break;                }            }            else                flag = true;            if (!flag)                break;            bool fg = false;            sort(a, a + i, cmp);            for (k = m.begin(); k != jend; k++){                //cout << (*k).first << endl;                if (strcmp((*k).first , a)==0){for(j=0;j<strlen((*k).second);j++){d[t][j]=(*k).second[j];}d[t++][j]='\0';                    fg = true;                }            }for(j=0;j<t-1;j++){for(i=j+1;i<t;i++){if(strcmp(d[j],d[i])>0){char *dd=new char[strlen(d[j])];strcpy(dd,d[j]);strcpy(d[j],d[i]);strcpy(d[i],dd);}}}for(j=0;j<t;j++){for(i=0;i<strlen(d[j]);i++){cout<<d[j][i];}cout<<endl;}            if (!fg){                cout << "NOT A VALID WORD" << endl;                            }                        for (i = 0; i < 6; i++){                cout << "*";            }            cout << endl;        }        return 0;}

 
 
0 0
原创粉丝点击