Word Amalgamation

来源:互联网 发布:用友软件工资 编辑:程序博客网 时间:2024/05/16 01:04

Word Amalgamation

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


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******
方法一:
 
/*思路:我的思路很笨,先排序,再一个一个的匹配*/#include<iostream>#include<cstdio>using namespace std;int cmp(const void *a,const void *b){//按字典升序排序char *c=(char *)a;char *d=(char *)b;for(int i=0;i<strlen(c);i++)if(c[i]!=d[i])return c[i]-d[i];return c[0]-d[0];}int main(){char a[222][8];char b[8];int k=0;while(gets(a[k++])&&strcmp(a[k-1],"XXXXXX"));qsort(a,k,sizeof(a[0]),cmp);while(gets(b)&&strcmp(b,"XXXXXX")){int vi[10];int tag=0,tag1=0;for(int i=0;i<k;i++){tag=0;memset(vi,0,sizeof(vi));if(strlen(a[i])==strlen(b)){for(int j=0;j<strlen(a[i]);j++){tag=0;for(int l=0;l<strlen(b);l++)if(a[i][j]==b[l]&&!vi[l]){vi[l]=1;tag=1;break;}if(!tag)break;}}if(tag){tag1=1;cout<<a[i]<<endl;}}if(!tag1)cout<<"NOT A VALID WORD"<<endl;cout<<"******"<<endl;}return 0;}
方法二:
/*思路:使用map容器,和string类的排序方法*/#include<iostream>#include<map>#include<string>#include<algorithm>using namespace std;map<string,string>m;int main(){string a;string end="XXXXXX";while(cin>>a&&a!=end){string t=a;sort(a.begin(),a.end());m[t]=a;}while(cin>>a&&a!=end){sort(a.begin(),a.end());int flag=0;for(map<string,string>::iterator it=m.begin();it!=m.end();it++)if(it->second==a){flag=1;cout<<it->first<<endl;}if(!flag)cout<<"NOT A VALID WORD"<<endl;cout<<"******"<<endl;}return 0;}