1. 程式人生 > >POJ 1318 Word Amalgamation(我的水題之路——亂序字母匹配)

POJ 1318 Word Amalgamation(我的水題之路——亂序字母匹配)

Word Amalgamation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6130 Accepted: 3051

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

Source

題意是說,有一個字串佇列,被定義為dictionary,dictionary的資料輸入將以“XXXXXX”結束,之後的將輸入一些字元字混亂的的字串,需要你按照字元的字典序將這個混亂字元中所有字母可以組成的字典中的字串輸出。 解法為,先對於字典中所有字串按照字典序排序,接著,將每個字串中的所有字母用sort排序,將每個字串均置換為字典序字串。然後將需要匹配的混亂字串,也用sort排序,排序後,去和字典中的亂碼比較,如果完全匹配則將其輸出。便可以得到字典序的字串了。 題目的注意點: 1)結果需要按照字典數輸出 程式碼(1AC1WA)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

char dictionary[200][10];

struct dict{
    int id;
    char dic[10];
}mydic[200];

void dicinit(int n){
    int i, j;
    int result;
    char tmp[10];
    int ttmp;

    for (i = 0; i < n; i++){
        strcpy(mydic[i].dic, dictionary[i]);
        mydic[i].id = i;
    }
    for (i = 0; i < n - 1; i++){
        for (j = 0 ; j < n - i - 1; j++){
            if (strcmp(mydic[j].dic, mydic[j + 1].dic) > 0){
                strcpy(tmp, mydic[j].dic);
                strcpy(mydic[j].dic, mydic[j + 1].dic);
                strcpy(mydic[j + 1].dic , tmp);
                ttmp = mydic[j].id;
                mydic[j].id = mydic[j + 1].id;
                mydic[j + 1].id = ttmp;
            }
        }
    }
    /*printf("DICTIONARY:\n");
    for (i = 0; i < n; i++){
        printf("%s\n", mydic[i].dic);
    }*/
    for (i = 0; i < n; i++){
        sort(mydic[i].dic, mydic[i].dic + strlen(mydic[i].dic));
    }
    /*printf("DICTIONARY:\n");
    for (i = 0; i < n; i++){
        printf("%s\n", mydic[i].dic);
    }*/
}

int main(void){
    char tmp[10];
    const char end[8] = "******";
    int i;
    int flag;
    int num = 0;

    i = num = 0;
    while (scanf("%s", tmp), strcmp(tmp, "XXXXXX") != 0){
        strcpy(dictionary[i++], tmp);
        num ++;
    }
    dicinit(num);
    while (scanf("%s", tmp), strcmp(tmp, "XXXXXX") !=  0){
        flag = 0;
        sort(tmp, tmp + strlen(tmp));
        for (i = 0; i < num ; i++){
            if (strcmp(mydic[i].dic, tmp) == 0){
                printf("%s\n", dictionary[mydic[i].id]);
                flag = 1;
            }
        }
        if (!flag){
            printf("NOT A VALID WORD\n");
        }
        printf("%s\n", end);
    }
    return 0;
}