UVA 642 - Word Amalgamation

 

In millions of newspapers across the United States there is a word game calledJumble. 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 file 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 sentinelXXXXXXcontains uppercaseX'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
******

 

 

 

#define RUN
#ifdef RUN

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int n;	// n为单词的总数

// word数组的每一行都存放着词典中的一个单词
char word[2000][10], sorted[2000][10];


/*
基本思想:
借组qsort函数,先把在词典中所有的单词排序,
再对每一个单词进行字母排序,
把结果放在sorted[]数组中
对于新输入需要判断的单词,直接和sorted数组里面的各个单词比较即可
*/

// 字符比较函数
int cmp_char(const void* _a, const void* _b) {
  char* a = (char*)_a;
  char* b = (char*)_b;
  return *a - *b;
}

// 字符串比较函数
int cmp_string(const void* _a, const void* _b) {
  char* a = (char*)_a;
  char* b = (char*)_b;
  return strcmp(a, b);
}


int main() {

#ifndef ONLINE_JUDGE
	freopen("642.in", "r", stdin);
	freopen("642.out", "w", stdout); 
#endif


  n = 0;		// n为单词总数
  for(;;) {
    scanf("%s", word[n]);	// 记录每一个定义的单词
    if(word[n][0] == 'X') break;	// 结束记录
    n++;
  }

  // 此步可省略
  qsort(word, n, sizeof(word[0]), cmp_string);
  
  // 对每一个单词进行字母层面的排序,使每一个单词都是alphabetacal
  for(int i = 0; i < n; i++) {
    strcpy(sorted[i], word[i]);
    qsort(sorted[i], strlen(sorted[i]), sizeof(char), cmp_char);
  }

  char s[10];
  while(scanf("%s", s)!=EOF && s[0]!='X') {
	// 对读入的每一个乱序单词进行排序
    qsort(s, strlen(s), sizeof(char), cmp_char);
    int found = 0;
	// 与词典库中的每一个已定义的单词进行比较
    for(int i = 0; i < n; i++){
      if(strcmp(sorted[i], s) == 0) {
        found = 1;
        printf("%s\n", word[i]);
      }
	}
    if(!found) printf("NOT A VALID WORD\n");
    printf("******\n");
  }
  return 0;
}

#endif



你可能感兴趣的:(word)