POJ 1035.Spell checker
Description
You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their forms.
If the word is absent in the dictionary then it can be replaced by correct words (from the dictionary) that can be obtained by one of the following operations:
?deleting of one letter from the word;
?replacing of one letter in the word with an arbitrary letter;
?inserting of one arbitrary letter into the word.
Your task is to write the program that will find all possible replacements from the dictionary for every given word.Input
The first part of the input file contains all words from the dictionary. Each word occupies its own line. This part is finished by the single character '#' on a separate line. All words are different. There will be at most 10000 words in the dictionary.
The next part of the file contains all words that are to be checked. Each word occupies its own line. This part is also finished by the single character '#' on a separate line. There will be at most 50 words that are to be checked.
All words in the input file (words from the dictionary and words to be checked) consist only of small alphabetic characters and each one contains 15 characters at most.Output
Write to the output file exactly one line for every checked word in the order of their appearance in the second part of the input file. If the word is correct (i.e. it exists in the dictionary) write the message: " is correct". If the word is not correct then write this word first, then write the character ':' (colon), and after a single space write all its possible replacements, separated by spaces. The replacements should be written in the order of their appearance in the dictionary (in the first part of the input file). If there are no replacements for this word then the line feed should immediately follow the colon.Sample Input
i is has have be my more contest me too if award # me aware m contest hav oo or i fi mre #Sample Output
me is correct aware: award m: i my me contest is correct hav: has have oo: too or: i is correct fi: i mre: more me
给一些合法单词和一些待测的单词,如果待测单词合法,则输出“xx is correct”。
否则,输出删去、修改、增加一个字符后合法的单词
输出按照输入顺序
有个题解说用Trie树,写的一直WA、TLE
换用暴力解
AC代码:GitHub
1 /* 2 By:OhYee 3 Github:OhYee 4 HomePage:http://www.oyohyee.com 5 Email:oyohyee@oyohyee.com 6 Blog:http://www.cnblogs.com/ohyee/ 7 8 かしこいかわいい? 9 エリーチカ! 10 要写出来Хорошо的代码哦~ 11 */ 12 13 #include <cstdio> 14 #include <algorithm> 15 #include <cstring> 16 #include <cmath> 17 #include <string> 18 #include <iostream> 19 #include <vector> 20 #include <list> 21 #include <queue> 22 #include <stack> 23 #include <map> 24 using namespace std; 25 26 //DEBUG MODE 27 #define debug 0 28 29 //循环 30 #define REP(n) for(int o=0;o<n;o++) 31 32 int ok; 33 int ldict,lstr; 34 string temp; 35 vector<string> dict; 36 37 38 void replace(string dict) { 39 int count = 0; 40 for(size_t i = 0; i < dict.length(); i++) { 41 if(temp[i] != dict[i]) count++; 42 if(count > 1) return; 43 } 44 ok = 1; 45 cout << " " + dict; 46 } 47 48 void insert(string dict) { 49 string tmp; 50 for(size_t i = 0; i < temp.length(); i++) { 51 if(temp[i] != dict[i]) { 52 tmp = dict; 53 tmp.insert(i,1,temp[i]); 54 if(tmp == temp) { 55 ok = 1; 56 cout << " " + dict; 57 } 58 return; 59 } 60 } 61 } 62 63 void del(string dict) { 64 string tmp; 65 for(size_t i = 0; i < dict.length(); i++) { 66 if(temp[i] != dict[i]) { 67 tmp = dict; 68 tmp.erase(i,1); 69 if(tmp == temp) { 70 ok = 1; 71 cout << " " + dict; 72 } 73 return; 74 } 75 } 76 } 77 78 bool Do() { 79 while(1) { 80 if(!(cin >> temp)) 81 return false; 82 if(temp == "#") break; 83 dict.push_back(temp); 84 } 85 86 while(1) { 87 cin >> temp; 88 if(temp == "#") break; 89 90 ok = -1; 91 lstr = temp.length(); 92 93 for(size_t i = 0; i < dict.size(); i++) { 94 if(dict[i] == temp) { 95 cout << temp + " is correct"; 96 ok = 0; 97 break; 98 } 99 } 100 101 102 if(ok < 0) { 103 cout << temp + ':'; 104 for(size_t i = 0; i < dict.size(); i++) { 105 ldict = dict[i].length(); 106 if(lstr == ldict) { 107 replace(dict[i]); 108 } else if(lstr == ldict + 1) { 109 insert(dict[i]); 110 } else if(lstr == ldict - 1) { 111 del(dict[i]); 112 } 113 } 114 } 115 116 cout << endl; 117 } 118 119 return true; 120 } 121 122 int main() { 123 while(Do()); 124 return 0; 125 }
然而,我并不能保证我说的是对的。请自行验证,如有错误,请指正
新博客地址
https://www.oyohyee.com
https://www.oyohyee.com