HDU 1113 Word Amalgamation

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. 
 

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;
}
2.对于与输入字符串长度相同的单词,记录其中26个字母出现的次数,然后记录输入字符串26个字母出现的次数,再进行比较。

代码:

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值