HDoj 1113 Word Amalgamation【1つの文字列が1つの文字列セットの文字列に変換されて得られるか否かを判断する】

3616 ワード


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

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

 
題意:文字列セットを与え、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
"); else { sort(num, num+t, cmp); for(i = 0; i < t; i++) printf("%s
", num[i].st); } printf("******
"); } return 0; }