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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章