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.
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.
tarp given score refund only trap work earn course pepper part XXXXXX resco nfudre aptr sett oresuc XXXXXX
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; }