Word Amalgamation(poj1318) 解题报告

来源:互联网 发布:怎么看java源码 编辑:程序博客网 时间:2024/09/21 09:02

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

解题思路:主要是和知道的字符串进行匹配,选出符合要求的,按题目要求对字典库进行排序,然后 就是输入数据的问题了,要和他们一个个比较,可以对他们字符串排序,这样就方便操作,同时快速 解决问题,只要对他们字符串对比就知道了,没什么困难的。  代码如下: 
Code:
  1. #include<iostream>  
  2. #include <algorithm>  
  3. using namespace std;  
  4. char data[105][10],s[10],temp[10];  
  5.   
  6. int cmp(const void *a,const void *b)  
  7. {  
  8.      return(strcmp((char*)a,(char*)b));  
  9. }  
  10.   
  11. int main()  
  12. {  
  13.     int i=0;  
  14.     while(1)  
  15.     {  
  16.         cin>>data[i];  
  17.         if(data[i][0]=='X')  
  18.             break;  
  19.         else  
  20.             i++;  
  21.     }  
  22.     qsort(data,i,sizeof(data[0]),cmp);  
  23.     while(1)  
  24.     {  
  25.         cin>>s;  
  26.         if(s[0]=='X')  
  27.             break;  
  28.         sort(s,s+strlen(s));  
  29.         int flag=0;  
  30.         for(int j=0;j<i;j++)  
  31.         {  
  32.             strcpy(temp,data[j]);  
  33.             sort(temp,temp+strlen(temp));  
  34.             if(!strcmp(s,temp))  
  35.             {  
  36.                 flag=1;  
  37.                 cout<<data[j]<<endl;  
  38.             }  
  39.         }  
  40.         if(flag==0)  
  41.             cout<<"NOT A VALID WORD"<<endl;  
  42.         cout<<"******"<<endl;  
  43.     }  
  44.     return 0;  
  45. }