Word Amalgamation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2845 Accepted Submission(s): 1369
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
******
Source
Recommend
<1>输入几个字符串,(按字典序排好)→ 用作字典;
<2>输入无序单词,查找;
<3>输出;
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 struct dic 7 { 8 int n ; 9 char str[10]; 10 }; 11 dic num[110]; 12 13 bool cmp(char c, char b) 14 { 15 return c > b; 16 } 17 18 bool cpm(dic str, dic n) 19 { 20 return strcmp(str.str, n.str) < 0; 21 } 22 int main() 23 { 24 char ch[10]; 25 char s[10]; 26 char ac[110][10]; 27 int i, j,total=0; 28 while(~scanf("%s",ch)&&strcmp(ch, "XXXXXX")!=0) 29 { 30 strcpy(num[total++].str, ch); 31 //i++; 32 } 33 sort(num, num+total, cpm); 34 //printf("%d\n", total); 35 while(~scanf("%s",ch),strcmp(ch, "XXXXXX")!=0) 36 { 37 int flag=0 ,len; 38 int nel=strlen(ch); 39 sort(ch, ch+nel, cmp); 40 for(i=0; i<=total; i++) 41 { 42 strcpy(s, num[i].str); 43 len = strlen(s); 44 sort(s, s+len, cmp); 45 if(strcmp(s, ch) == 0) 46 { 47 printf("%s\n", num[i].str); 48 flag++; 49 } 50 //else 51 //printf("NOT A VALID WORD\n"); 52 } 53 if(flag==0) 54 printf("NOT A VALID WORD\n"); 55 /*else 56 { 57 sort(ac,ac+flag,cpm); 58 for(i=0; i<flag; i++) 59 printf("%s", ac[i]); 60 }*/ 61 printf("******\n"); 62 } 63 return 0; 64 }