UVA 642 - Word Amalgamation
2013-03-03 03:48
302 查看
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.
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; and4.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.
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.
#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
); // 记录每一个定义的单词
if(word
[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
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; and4.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 ona 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
); // 记录每一个定义的单词
if(word
[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
相关文章推荐
- HDU1113 POJ1318 UVA642 ZOJ1181 UVALive5328 Word Amalgamation【MAP+排序+水题】
- UVa642 - Word Amalgamation
- UVa 642 Word Amalgamation
- UVa 642 Word Amalgamation (查字典&字符串排序)
- UVa 642 - Word Amalgamation
- UVa 642 - Word Amalgamation
- UVa 642 - Word Amalgamation
- Uva 642 - Word Amalgamation sort qsort
- POJ 1318 Word Amalgamation (字符串 STL大水)
- uva 1401 && Live Archive 3942 Remember the Word
- ACM--字符串--HDOJ 1113--Word Amalgamation--map
- uva 422 Word-Search Wonder
- Word Amalgamation(枚举 + 排序)
- uva 1401 - Remember the Word(字典树)
- POJ 1318 Word Amalgamation 笔记
- UVA-1401 - Remember the Word
- UVA 417 Word Index
- POJ-1318 Word Amalgamation
- hdu 1113 Word Amalgamation
- UVALive 3942 Remember the word 字典树 动态规划