• Word Amalgamation


    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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

    字符串处理问题,比较简单,用到了排序和查找。

    题意:以第一次"XXXXXX"出现的地方为分隔,上面是字典里的单词,下面是file。

    将file里面的每个单词排序后与字典里的单词排序后相比较,只要相同即输出字典里的单词。

    刘汝佳的书里有“字母重排”

     

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #define M 1000
    char word[2000][10],sorted[2000][10];
    int cmp_string(const void *a,const void *b)//字符比较函数 
    {
        char *x=(char *)a;
        char *y=(char *)b;
        return *x-*y;
    }
    int cmp_char(const void *a,const void *b)//字符串比较函数 
    {
        char *x=(char *)a;
        char *y=(char *)b;
        return strcmp(x,y);
    }
    int main()
    {
        int n=0;
        while(1)
        {
            scanf("%s",word[n]);
            if(word[n][0]=='X')//遇到结束标志就终止循环 
               break;
            n++;
        }
        qsort(word,n,sizeof(word[0]),cmp_string);//给所有单词排序 
        for(int 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)!=EOF)//持续读取到文件结束 
        {
            if(s[0]=='X')
               break;
            qsort(s,strlen(s),sizeof(char),cmp_char);//给输入单词排序 
            int found=0;
            for(int i=0;i<n;i++)
            {
                if(strcmp(sorted[i],s)==0)
                {
                    found=1;
                    printf("%s
    ",word[i]);//输出原始单词,而不是排序后的 
                }
            }
            if(!found)
               printf("NOT A VALID WORD
    ");
            printf("******
    ");
        }
        return 0;
    }

     

     

  • 相关阅读:
    中序遍历【递归算法】和【非递归算法】
    等价无穷小替换
    轮转访问MAC协议
    曲率
    Java I/O流 01
    Java 集合框架 04
    Java 集合框架 03
    Java 集合框架 02
    Java 集合框架 01
    Java 常见对象 05
  • 原文地址:https://www.cnblogs.com/lipching/p/3867238.html
Copyright © 2020-2023  润新知