hdoj 1113 Word Amalgamation 【判断一个字符串能否被一个字符串集里的字符串经过变换而得到】

来源:互联网 发布:穆逢春 知乎 编辑:程序博客网 时间:2024/05/21 18:30


Word Amalgamation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2810    Accepted Submission(s): 1351


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
tarpgivenscorerefundonlytrapworkearncoursepepperpartXXXXXXresconfudreaptrsettoresucXXXXXX
 

Sample Output
score******refund******parttarptrap******NOT A VALID WORD******course******
 
题意:给出一个字符串集,以XXXXXX结束。再给出一系列目标字符串,也以XXXXXX结束。判断目标字符串能否被字符串集里的字符串经过变换而得到,若不能输出
NOT A VALID WORD,反之 按字典序输出那些可以变换得到目标字符串的字符串,每个目标字符串测试结束输入******。

思路:按照字典序变换并记录 字符串集里面的每个字符串,对于目标字符串 也按字典序排列,并找出 与 排序后的目标字符串 相等的串存储其变换前对应字符串,按字典序排序后输出即可。

代码:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct rec{char st[10];}num[110];char word[110][10];//输入的单词 char convert[110][10];//变换后的单词 char str[10];//查询的单词int vis[110];//标记已经用过的单词 bool cmp(rec a, rec b)//按照字典序排列 字符串 {return strcmp(a.st, b.st) < 0;} int main(){int n = 0;int i;int t, exist;//记录找到的字符串数目 和 当前字符串是否能找到匹配串 while(scanf("%s", word[n]), strcmp(word[n], "XXXXXX")){vis[n] = 0; strcpy(convert[n], word[n]);sort(convert[n], convert[n] + strlen(convert[n]));n++;}while(scanf("%s", str), strcmp(str, "XXXXXX")){t = 0;//记录匹配的字符串数目 exist = 0;//标记是否找到 for(i = 0; i < n; i++){if(vis[i]) continue;sort(str, str+strlen(str));if(strcmp(str, convert[i]) == 0){exist = 1;strcpy(num[t++].st ,word[i]);}}if(!exist)printf("NOT A VALID WORD\n");else{sort(num, num+t, cmp);for(i = 0; i < t; i++)printf("%s\n", num[i].st);}printf("******\n");} return 0;}

0 0
原创粉丝点击