Word Amalgamation 的思考

本文介绍了一种解决Jumble单词游戏的算法实现,通过字典排序与字母重组对比,快速找出正确答案。

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

题目描述

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.

输入

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.

输出

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.

样例输入

tarp
given
score
refund
only
trap
work
earn
course
pepper
part
XXXXXX
resco
nfudre
aptr
sett
oresuc
XXXXXX

样例输出

score
******
refund
******
part
tarp
trap
******
NOT A VALID WORD
******
course
******

     就是先输入一个字典然后在输入一些乱码的单词,找出字典中"对应"的单词并输出,如果有多个那就按字典排序输出,如果没有就输出一行NOT A VALID WORD 当一个单词搜索完后不论找没找到都要输出一行******表示该单词的搜索结束。

   刚开始的时候,这个按字典排序输出的真的弄的我一懵,这可咋整?  其实应该在字典输入完成后就进行字典排序,并且要用到sort函数,且当sort函数对字符串之间排序时,还是用结构体外壳比较好,比较函数也比较好写。

其次是:输入一些乱码的单词,找出字典中"对应"的单词,这个问题也是难住我这菜逼了,其实可以将字典和乱码中的每个字符串都用sort排序,这样如果两者是相同的,那么sort排序后就应当也是一样的,这样只要用strcmp()判断即可。

               AC后发现这道题其实就是sort的应用

AC:

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    char st[10];
    char end[10];
};
bool cmp(node tt,node ttt)
{
    return strcmp(tt.st,ttt.st)<0;
}
  node a[200],b[50];
int main()
{
    int i,t,k=0,j=0;
    while(cin>>a[j].st&&strcmp(a[j].st,"XXXXXX")!=0)
    {
        strcpy(a[j].end,a[j].st);
        int l=strlen(a[j].st);
        sort(a[j].end,a[j].end+l);
        j++;
    }
    sort(a,a+j,cmp);
    while(cin>>b[k].st&&strcmp(b[k].st,"XXXXXX")!=0)
    {
        strcpy(b[k].end,b[k].st);
        int l=strlen(b[k].st);
        sort(b[k].end,b[k].end+l);
        k++;
    }
    for (i=0;i<k;i++)
        {
            int judge=0;
          for (t=0;t<j;t++)  
          {
           if (strcmp(b[i].end,a[t].end)==0)
           {
               cout<<a[t].st<<endl;
                judge++;
           }
          }
          if (!judge)
             cout<<"NOT A VALID WORD"<<endl;
          cout<<"******"<<endl;
        }
       return 0;
}

      今天就要写题了,数据结构实验周四就要考了,感觉要凉啊, 希望有点用吧!

         18-7-3

Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5522 Accepted: 2789 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 ****** Source Mid-Central USA 1998
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值