Word Amalgamation(hdoj1113)
Word Amalgamation
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.
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
tarp
given
score
refund
only
trap
work
earn
course
pepper
part
XXXXXX
resco
nfudre
aptr
sett
oresuc
XXXXXX
Sample Output
score
******
refund
******
part
tarp
trap
******
NOT A VALID WORD
******
course
******
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int cmp(int n,char *p,char *q) 7 { 8 sort(p,p+n); 9 sort(q,q+n); 10 if(0==strcmp(p,q)) 11 return 1; 12 else 13 return 0; 14 } 15 int main() 16 { 17 char a[102][7],b[102][7],c[102][7],i=0,j,count1,count2; 18 bool flags=0; 19 // freopen("in.txt","r",stdin); 20 while(gets(a[i])) 21 { 22 if(!strcmp(a[i],"XXXXXX")) 23 break; 24 else 25 i++; 26 } 27 count1=i,i=0;/*个数*/ 28 while(gets(b[i])) 29 { 30 if(!strcmp(b[i],"XXXXXX")) 31 break; 32 else 33 i++; 34 } 35 count2=i; 36 for(i=0;i<count2;i++) 37 { 38 int k=0; 39 int lenb=strlen(b[i]); 40 char bb[7]; 41 strcpy(bb,b[i]); 42 for(j=0;j<count1;j++) 43 { 44 int lena=strlen(a[j]); 45 char aa[7]; 46 strcpy(aa,a[j]); 47 if((lena==lenb)&&cmp(lena,bb,aa)) 48 { 49 strcpy(c[k],a[j]); 50 k++; 51 flags=1; 52 } 53 } 54 char uu[7]; 55 int p; 56 for(j=0;j<k-1;j++) 57 for(p=j+1;p<k;p++) 58 { 59 if(strcmp(c[j],c[p])>=0) 60 { 61 strcpy(uu,c[j]); 62 strcpy(c[j],c[p]); 63 strcpy(c[p],uu); 64 } 65 } 66 for(j=0;j<k;j++) 67 cout<<c[j]<<endl; 68 if(flags) 69 cout<<"******"<<endl; 70 else 71 cout<<"NOT A VALID WORD"<<endl<<"******"<<endl; 72 flags=0; 73 } 74 }