HDU1113 Word Amalgamation

灰太狼 2022-06-09 13:11 193阅读 0赞

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

  1. tarp
  2. given
  3. score
  4. refund
  5. only
  6. trap
  7. work
  8. earn
  9. course
  10. pepper
  11. part
  12. XXXXXX
  13. resco
  14. nfudre
  15. aptr
  16. sett
  17. oresuc
  18. XXXXXX

Sample Output

  1. score
  2. ******
  3. refund
  4. ******
  5. part
  6. tarp
  7. trap
  8. ******
  9. NOT A VALID WORD
  10. ******
  11. course
  12. ******

Attention:对若干个字符串进行字典排序,应用结构体数组储存字符串(二维数组会报错),以用函数 sort 进行排序。

Code:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <ctype.h>
  9. using namespace std;
  10. char wd[100][15],wd_sort[100][15];
  11. struct w
  12. {
  13. char res[15];
  14. }wd_rs[100];
  15. bool cmp(struct w a,struct w b)
  16. {
  17. return strcmp(a.res,b.res)<0;
  18. }
  19. int main()
  20. {
  21. char wt[15];
  22. int len=0;
  23. while(scanf("%s",wt)&&strcmp(wt,"XXXXXX"))
  24. {
  25. strcpy(wd[len],wt);
  26. strcpy(wd_sort[len],wd[len]);
  27. len++;
  28. }
  29. int k=len;
  30. while(k--)
  31. {
  32. sort(wd_sort[k],wd_sort[k]+strlen(wd_sort[k]));
  33. }
  34. char w[15];
  35. while(scanf("%s",w)&&strcmp(w,"XXXXXX"))
  36. {
  37. sort(w,w+strlen(w));
  38. int flag=0,z=0;
  39. k=len;
  40. for(int i=0;i<=k;i++)
  41. {
  42. if(strcmp(wd_sort[i],w)==0)
  43. {
  44. strcpy(wd_rs[z++].res,wd[i]);
  45. flag=1;
  46. }
  47. }
  48. if(!flag)
  49. printf("NOT A VALID WORD\n");
  50. else
  51. {
  52. sort(wd_rs,wd_rs+z,cmp);
  53. for(int i=0;i<z;i++)
  54. printf("%s\n",wd_rs[i].res);
  55. }
  56. printf("******\n");
  57. }
  58. return 0;
  59. }

发表评论

表情:
评论列表 (有 0 条评论,193人围观)

还没有评论,来说两句吧...

相关阅读