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 shocould 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 shoshould 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
Question:
Specify a dictionary to determine whether the input number is in the dictionary.
If you do not use the dictionary, you can use one of the following three variants to convert the string into a dictionary.
1) Add one character. 2) delete one character. 3) change one character.
Solution:
String processing, which is not in the dictionary first.
If not, judge whether it can be changed to a character string in the dictionary. Pay attention to the statement writing method.
Code:
1 /************************************** * *********************************** 2> File Name: poj1035.cpp 3> author: enumz 4> mail: [email protected] 5> created time: october 16, Sunday, October 19, 2014, 6 ******************************* **************************************** */7 8 # include <iostream> 9 # include <cstdio> 10 # include <cstdlib> 11 # include <string> 12 # include <cstring> 13 # include <list> 14 # inclu De <queue> 15 # include <stack> 16 # include <map> 17 # include <set> 18 # include <algorithm> 19 # define maxn 1000 20 using namespace STD; 21 char DIC [100000] [100]; 22 char word [1000000]; 23 int num_dic; 24 bool find_same (char * Word) 25 {26 bool OK = 0; 27 For (INT I = 1; I <= num_dic; I ++) 28 If (strcmp (DIC [I], word) = 0) 29 {30 OK = 1; 31 break; 32} 33 Return OK; 34} 35 void find_opt (char * Word) 36 {37 for (in T I = 1; I <= num_dic; I ++) 38 {39 int len_word = strlen (Word); 40 int len_dic = strlen (DIC [I]); 41 if (len_word = len_dic) 42 {43 int CNT = 0; 44 for (Int J = 0; j <= len_word-1; j ++) 45 if (DIC [I] [J]! = Word [J]) CNT ++; 46 If (CNT = 1) 47 printf ("% s", DIC [I]); 48} 49 else if (len_word-len_dic = 1) 50 {51 int k1 = 0, K2 = 0, CNT = 0; 52 while (1) 53 {54 if (k1 = len_dic & k2 = len_word) 55 break; 56 If (CNT> = 2) 57 break; 58 If (DIC [I] [k1] = word [k2]) 59 K1 ++, K2 ++; 60 else K2 ++, CNT ++; 61} 62 if (CNT = 1) 63 printf ("% s", DIC [I]); 64} 65 else if (len_dic-len_word = 1) 66 {67 int k1 = 0, K2 = 0, CNT = 0; 68 while (1) 69 {70 If (k1 = len_dic & k2 = len_word) 71 break; 72 If (CNT> = 2) 73 break; 74 if (DIC [I] [k1] = word [k2]) 75 K1 ++, K2 ++; 76 else K1 ++, CNT ++; 77} 78 If (CNT = 1) 79 printf ("% s", DIC [I]); 80} 81} 82} 83 int main () 84 {85 num_dic = 1; 86 while (1) 87 {88 scanf ("% s", DIC [num_dic]); 89 If (strcmp (DIC [num_dic], "#") = 0) 90 {91 num_dic --; 92 break; 93} 94 else num_dic ++; 95} 96 while (1) 97 {98 scanf ("% s", word); 99 If (strcmp (word, "#") = 0) 100 break; 101 if (find_same (Word) 102 printf ("% s is correct \ n", word); 103 else 104 {105 printf ("% s:", word ); 106 find_opt (Word); 107 printf ("\ n"); 108} 109} 110 return 0; 111}
Poj1035 -- spell checker (string processing)