POJ1318解题报告

来源:互联网 发布:2015淘宝新店扶持多久 编辑:程序博客网 时间:2024/06/05 09:40
 
Word Amalgamation
Time Limit: 1000MS Memory Limit: 10000KTotal 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

tarpgivenscorerefundonlytrapworkearncoursepepperpartXXXXXXresconfudreaptrsettoresucXXXXXX

Sample Output

score******refund******parttarptrap******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;    }

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 支付宝向对方发起收款不付怎么办 支付宝收款别人少付了怎么办 红牛领到50元加油优惠券怎么办用 苹果淘宝看评价图片看不清楚怎么办 淘宝账号账户体验中心有违规怎么办 淘宝买的东西需要寄回去维修怎么办 淘宝闪电退款了卖家不肯退了怎么办 云视听会员账号账号密码忘了怎么办 移动卡异地补卡忘记服务密码怎么办 欠我钱的人不接电话怎么办 苹果手机换屏升级系统了黑屏怎么办 苹果手机黑屏时接不到电话怎么办 手机上打电话的图标没了怎么办啊 阿巴町手表上的二维码不生成怎么办 速卖通几个月来搜索曝光很差怎么办 你我贷逾期3个月怎么办 手机放声音的地方进水了怎么办 手机放卡的地方坏了怎么办 京东购物申请退款卖家不同意怎么办 京东申请退货退款卖家不处理怎么办 京东自营产品收货地址填错了怎么办 京东快递地址填错了怎么办? 拼多多网上购物收不到东西怎么办 网上购物己签收但东西不好怎么办 孩子在学校学习用具总是被偷怎么办 歌华有线欠费1个月怎么办 唐小僧倒闭了百姓投的钱怎么办? 手机号被别人注册了华为账号怎么办 华为账号手机号显示已被注册怎么办 买了鑫和陌车的怎么办 注册游戏账号时乱输入的邮箱怎么办 yy频道解邦不能开直播怎么办 淘宝的淘金币快过期了怎么办 乐透啦彩票让骗了6万怎么办 交了认筹金不能进抢购平台怎么办 爱奇艺会文学会员办了想退款怎么办 海淘信用卡入账但是砍单怎么办 褐色分泌物流了好几天了怎么办? 淘宝买的衣服一直不发货怎么办 从国外寄东西到国内被税了怎么办 百度网盘上传文件数量有限制怎么办