POJ1318解题报告

 
Word Amalgamation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6105 Accepted: 3041

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
******
核心思想:查找在字典中含有的字母。首先将字典进行按字母升序进行排序,将词表也按照字母的升序进行排序。然后看字母在词典中是否存在。对于存在的词,进行排序后再输出。
代码如下:(力求能够写好C程序,呵呵,对JAVA比较熟,但用JAVA处理字符串算是取巧了...)
 
#include<stdio.h>
#include<string.h>
#define MAXNUM 100
#define MAXLEN 6
//存储词的字典 
char *dictinary[MAXNUM];
char *words[MAXNUM];
//存放排序后的字典 
char* trans[MAXNUM];
char* record[MAXNUM]; 
int partion(char *str,int i,int j)
{
     int m=i,n=j+1;
     char ch=str[i];
     char temp;
     while(1){
     
           while(str[++m]<ch&&m<j);
           while(str[--n]>ch);
           if(m>=n)
           {
                   break;
           }
           temp=str[m];
           str[m]=str[n];
           str[n]=temp;
     } 
     str[i]=str[n];
     str[n]=ch;
     return n;
}
void QuickSort(char *str,int i,int j)
{
      if(i<j)
      {
             int p=partion(str,i,j);
             QuickSort(str,i,p-1);
             QuickSort(str,p+1,j);
      }
}
int main()
{
    //len1和len2与真实的长度都相差1 
    int len1=-1;
    int len2=-1;
    int index=-1;
    int i;
    int j;
    int k;
    int tag;
    char *te;
    te=(char*)malloc(sizeof(char)*MAXLEN);
    
    for(i=0;i<MAXNUM;i++)
    {
          record[i]=(char*)malloc(sizeof(char)*MAXLEN);
    }
    while(1)
    {
          dictinary[++len1]=(char*)malloc(sizeof(char)*MAXLEN);
          trans[len1]=(char*)malloc(sizeof(char)*MAXLEN);
          scanf("%s",dictinary[len1]);
          //做一个备份 
          strcpy(trans[len1],dictinary[len1]); 
          if(strcmp(dictinary[len1],"XXXXXX")==0)
          {
                  len1--;
                  break;
          }
    } 
    while(1)
    {
           words[++len2]=(char*)malloc(sizeof(char)*MAXLEN);
           scanf("%s",words[len2]);
           if(strcmp(words[len2],"XXXXXX")==0)
           {
                   len2--;
                   break;
           }
    }
    //对字典和词表按字母的升序进行排序
    for(i=0;i<=len1;i++)
    {
           QuickSort(trans[i],0,strlen(dictinary[i])-1);
    }
    for(i=0;i<=len2;i++)
    {
           QuickSort(words[i],0,strlen(words[i])-1);
    }
    //输出
    for(i=0;i<=len2;i++)
    {
          tag=1;
          index=-1;
          for(j=0;j<=len1;j++)
          {
                   if(strcmp(words[i],trans[j])==0)
                   {
                       tag=0;
                       record[++index]=dictinary[j];
                   }
          } 
          if(tag)
          {
                 printf("NOT A VALID WORD\n");
          }
          else
          {
                 //冒泡排序
                 for(j=0;j<=index-1;j++)
                 {
                    for(k=0;k<=index-1-j;k++)
                    {
                        if(strcmp(record[k],record[k+1])>0)
                        {
                              strcpy(te,record[k]);
                              strcpy(record[k],record[k+1]);
                              strcpy(record[k+1],te);
                        }
                    }
                 } 
                 for(j=0;j<=index;j++)
                 {
                     printf("%s\n",record[j]);
                 }
          }
          printf("******\n");
    } 
     
    free(te);
    free(dictinary);
    free(words);
    free(trans);
    system("pause");
    return 0;    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值