1035-Spell checker (fuzzy match), 1035-spellchecker
I. Question:
Give a set of dictionary words, end with '#', then give a set of word sequences for fuzzy match, end '#'
1. If a word can be found in the dictionary, corret is output.
2. If a word can be searched in the dictionary after being transformed, deleted, or added, the words are output. The output order is based on the Lexicographic Order of the dictionary entered.
3. If a word cannot be found in the dictionary no matter whether it is operated or not, the output is null.
Ii. Ideas:
Brute force simulation.
1. Input, ended '#'
2. determine the length of the dictionary word and the matched word.
I. If the word length is equal to the dict length, two strings may match, or one character may be matched after modification.
Ii. Otherwise, if the word length is 1 longer than that of dict, the system checks whether a character is matched after it is deleted.
Iii. Otherwise, if the length of dict is equal to the length of word, the system checks whether a character is matched after being added.
3. output.
Step 3:
1. input.
2. Judgment:
I, if strlen (word []) = strlen (dict [])
If! Strcmp (word [I], dict [j]), output corret.
Else if the number of different characters between them is <= 1, the array subscript of the word is saved to ans []
Ii, else if strlen (word [])-strlen (dict []) = 1
If the number of different characters between them is <= 1, the array subscript of the word is saved to ans []
Iii, else if strlen (dict [])-strlen (word [] = 1
If the number of different characters between them is <= 1, the array subscript of the word is saved to ans []
3. output.
1 # include <iostream> 2 # include <cstring> 3 using namespace std; 4 5 char dict [0, 10001] [16]; // store the dictionary 6 char word [51] [16]; // store the words to match 7 int dictNum = 0; // the number of words in the dictionary 8 int wordNum = 0; // The number of words to be matched 9 int dictLen [10001]; // The length of each word is 10 int ans [10001]; // store the location of each word in the dictionary 11 12 // convert whether a character is the same 13 bool change (char word [], char dict []) {14 int count = 0; 15 for (int I = 0; I <strlen (word); I ++) {16 if (word [I]! = Dict [I]) {17 count ++; 18 if (count> 1) {// different letters cannot exceed 1 19 return false; 20} 21} 22} 23 return true; 24} 25 26 // Delete whether a character is the same 27 bool del (char word [], char dict []) {28 int count = 0; 29 for (int I = 0, j = 0; I <strlen (word); I ++) {// word length> dict length 30 if (word [I]! = Dict [j]) {// if it is not equal to, word [] moves a 31 count ++ to the backend; 32 if (count> 1) {33 return false; 34} 35} 36 else {// otherwise word [], dict [] are moved back to a position 37 j ++; 38} 39} 40 return true; 41} 42 43 // add whether a character is the same 44 bool add (char word [], char dict []) {45 int count = 0; 46 for (int I = 0, j = 0; I <strlen (dict); j ++) {// dict length> word length 47 if (word [I]! = Dict [j]) {// if the value is not the same, dict [] moves one digit to the backend for 48 count ++; 49 if (count> 1) {50 return false; 51} 52} 53 else {// otherwise word [], dict [] Move back one position 54 I ++; 55} 56} 57 return true; 58} 59 60 // main work 61 void work (char dict [] [16], char word [] [16]) {62 for (int I = 0; I <dictNum; I ++) {63 dictLen [I] = strlen (dict [I]); 64} 65 for (int I = 0; I <wordNum; I ++) {66 memset (ans, 0, sizeof (ans); 67 int len = strlen (word [I ]); 68 bool flag = false; // The flag is divided into 69 int k = 0; 70 for (int j = 0; j <dictNum; j ++) {71 if (dictLen [j] = len) {// Change or Equal 72 if (! Strcmp (word [I], dict [j]) {73 flag = true; // if the first condition is met, 74 break is true; 75} 76 else if (change (word [I], dict [j]) {77 ans [k ++] = j; // if the same ans [] stores the word in the dictionary position 78} 79} 80 else if (len-dictLen [j] = 1) {// Delete 81 if (del (word [I], dict [j]) {82 ans [k ++] = j; 83} 84} 85 else if (dictLen [j]-len = 1) {// Add 86 if (add (word [I], dict [j]) {87 ans [k ++] = j; 88} 89} 90} 91 if (flag) {92 cout <Word [I] <"is correct" <endl; 93} 94 else {95 cout <word [I] <":"; 96 for (int j = 0; j <k; j ++) {97 cout <dict [ans [j] <''; 98} 99 cout <endl; 100} 101} 102 103 int main () {104 // input, end with '#'. 106 while (cin> dict [dictNum] & dict [dictNum ++] [0]! = '#'); 107 while (cin> word [wordNum] & word [wordNum ++] [0]! = '#'); 108 dictNum --; // when stored, do not save '#' 109 wordNum --; 110 work (dict, word); 111 return 0; 112}View Code
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.