Word Amalgamation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1708 Accepted Submission(s): 787
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
Eddy
分析:题意就不说了,蛮简单的,但是要注意输出要按字典顺序输出,所以前面供查找的一系列单词要先按字典顺序排序,再用来比较。
可以有两种想法:1.对字典里的每个单词按字母进行排序,输入的字符串也按字母进行排序,然后进行排序。
#include<stdio.h> #include<stdlib.h> #include<string.h> int n; char word[2000][10],sorted[2000][10]; int cmp_char(const void* _a,const void* _b) { char* a=(char *)_a; char* b=(char *)_b; return *a-*b; } int cmp_string(const void* _a,const void* _b) { char* a=(char*)_a; char* b=(char*)_b; return strcmp(a,b); } int main() { n=0; for(;;) { scanf("%s",word[n]); if(word[n][0]=='X')break; n++; } qsort(word,n,sizeof(word[0]),cmp_string); int i; for(i=0;i<n;i++) { strcpy(sorted[i],word[i]); qsort(sorted[i],strlen(sorted[i]),sizeof(char),cmp_char); } char s[10]; while(scanf("%s",s)==1) { if(strcmp(s,"XXXXXX")==0) break; qsort(s,strlen(s),sizeof(char),cmp_char); int found=0; for(i=0;i<n;i++) if(strcmp(sorted[i],s)==0) { found=1; printf("%s\n",word[i]); } if(!found) { printf("NOT A VALID WORD\n"); printf("******\n"); } else printf("******\n"); } return 0; }
代码:
#include<stdio.h> #include<string.h> char str[105][10]; char a[10]; int b[26],c[26],d[26]; char temp[10]; int main() { int len=0,i,j; while(scanf("%s",str[len])) { if(strcmp(str[len],"XXXXXX")==0) break; len++; } for(i=0;i<len;i++) for(j=i+1;j<len;j++) { if(strcmp(str[i],str[j])>0) { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } while(scanf("%s",a)) { if(strcmp(a,"XXXXXX")==0) break; int k,m,flag,flag1=0; int len1,len2; len1=strlen(a); for(j=0;j<len;j++) { len2=strlen(str[j]); if(len1==len2) { flag=0; memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); for(k=0;k<len1;k++) { b[a[k]-'a']++; c[str[j][k]-'a']++; } for(k=0;k<26;k++) { if(b[k]!=c[k]) { flag=1; break; } } if(flag==0) { flag1++; for(k=0;k<len1;k++) printf("%c",str[j][k]); printf("\n"); } } } if(flag1==0) { printf("NOT A VALID WORD\n"); printf("******\n"); } else printf("******\n"); } return 0; }