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