Word Amalgamation(poj1318) 解题报告

本文介绍了一款用于解开报纸游戏Jumble的程序设计思路及实现代码。该程序能够读取字典文件,并通过字符串匹配找出正确答案。文章详细解释了如何通过排序简化匹配过程,提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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
******


解题思路:主要是和知道的字符串进行匹配,选出符合要求的,按题目要求对字典库进行排序,然后 就是输入数据的问题了,要和他们一个个比较,可以对他们字符串排序,这样就方便操作,同时快速 解决问题,只要对他们字符串对比就知道了,没什么困难的。  代码如下: 
Code:
  1. #include<iostream>
  2. #include<algorithm>
  3. usingnamespacestd;
  4. chardata[105][10],s[10],temp[10];
  5. intcmp(constvoid*a,constvoid*b)
  6. {
  7. return(strcmp((char*)a,(char*)b));
  8. }
  9. intmain()
  10. {
  11. inti=0;
  12. while(1)
  13. {
  14. cin>>data[i];
  15. if(data[i][0]=='X')
  16. break;
  17. else
  18. i++;
  19. }
  20. qsort(data,i,sizeof(data[0]),cmp);
  21. while(1)
  22. {
  23. cin>>s;
  24. if(s[0]=='X')
  25. break;
  26. sort(s,s+strlen(s));
  27. intflag=0;
  28. for(intj=0;j<i;j++)
  29. {
  30. strcpy(temp,data[j]);
  31. sort(temp,temp+strlen(temp));
  32. if(!strcmp(s,temp))
  33. {
  34. flag=1;
  35. cout<<data[j]<<endl;
  36. }
  37. }
  38. if(flag==0)
  39. cout<<"NOTAVALIDWORD"<<endl;
  40. cout<<"******"<<endl;
  41. }
  42. return0;
  43. }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值