UVa 642 Word Amalgamation

Word Amalgamation

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

SampleInput 

tarp
given
score
refund
only
trap
work
earn
course
pepper
part
XXXXXX
resco
nfudre
aptr
sett
oresuc
XXXXXX

SampleOutput

score
******
refund
******
part
tarp
trap
******
NOT A VALID WORD
******
course
******


这个题WA的童鞋可以试试字典hello,输入heloo看看对不对。哥就败在这上面了大哭

正如一e文论坛上朋友说的,这个题属于简单题,基本上按样例输入时输出正确了就是AC的,如果确实错了,那就检查你的排序算法吧。


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

int compare_string(const void *a, const void *b)
{
    char *first = (char *)a;
    char *second = (char *)b;
    return strcmp(first, second);
}

int compare_chr(const void *a, const void *b)
{
    return *(char *)a - *(char *)b;
}

int main(int argc, const char * argv[]) {
    
    char dictionary[111][7];
    int countsOfDictionary = 0;
    /*保存字典*/
    while (scanf("%s", dictionary[countsOfDictionary])) {
        if (strcmp(dictionary[countsOfDictionary], "XXXXXX") !=0 ) {
            countsOfDictionary++;
        } else {
            break;
        }
    }
    
    int j, ok = 1;
    char word[7], *dictWord = NULL;
    char sortedDictWord[7];
    char correctWords[111][7];
    int correctCount = 0;
    
    while (scanf("%s", word)) {
        /*退出条件*/
        if (strcmp(word, "XXXXXX") == 0) {
            break;
        }
        correctCount = 0;
        for (j = 0; j < countsOfDictionary; j++) {
            
            ok = 1;
            
            dictWord = dictionary[j];
            if (strlen(word) != strlen(dictWord)) {
                continue;
            } else {<span style="white-space:pre">				</span>//判断是否存在某个字典单词可以由输入的字符串组成
                strcpy(sortedDictWord, dictWord);
                qsort(word, strlen(word), sizeof(char), compare_chr);
                qsort(sortedDictWord, strlen(sortedDictWord), sizeof(char), compare_chr);
                if (strcmp(sortedDictWord, word) != 0) {
                    ok = 0;
                }
            }
            
            if (ok) {<span style="white-space:pre">				</span>//如果有,保存起来一会排序输出。更好地做法是先排序到另一数组,后到原数组输出
                strcpy(correctWords[correctCount++], dictWord);
            }
            
        }
        
        if (correctCount == 0) {
            printf("NOT A VALID WORD\n");
        } else {
            //排序输出
            qsort(correctWords, correctCount, sizeof(correctWords[0]), compare_string);
            
            for (j = 0; j < correctCount; j++) {
                printf("%s\n", correctWords[j]);
            }
        }
        
        printf("******\n");
    }
    
    return 0;
}


你可能感兴趣的:(ACM,排序算法,uva)