POJ1035-Spell checker

Source: Internet
Author: User

 

Reprinted please indicate the source: Thank you

Http://user.qzone.qq.com/289065406/blog/1309051410

 

General question:

Enter a dictionary and several words

1. If a word can be found in the dictionary, corret is output.

2. If a word can passTransformOrDeleteOrAddAfter one character is found in the dictionary, 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.

 

Solution:

No difficulty in string processing, once AC

Brute force! Simulate it!

 

The basic idea is to compare the length of the word to be queried and the dictionary one by one, and check only when the absolute value of the difference between the two is <= 1.

 

Source correction:

Http://neerc.ifmo.ru/past/1998.html

 

 

// Memory time // 456 K 157 ms # include <iostream> # include <string. h> using namespace STD; char dict [10001] [16]; char word [51] [16]; int dictnum = 0; // dictionary counter int wordnum = 0; // word counter void input (void); bool change (char * word, char * dict); // check whether the string word can be converted to dictbool del (char * word, char * dict); // check whether the string word can be deleted to obtain dictbool add (char * word, char * dict); // check whether the string word can be added to obtain dictint main (void) {input (); int * dictlen = new int [DIC Tnum]; // calculate the length of each word in the dictionary for (INT n = 0; n <dictnum; n ++) dictlen [N] = strlen (dict [N]); For (INT I = 0; I <wordnum; I ++) {int * address = new int [dictnum]; // record the subscript int Pa = 0 of the word [I] obtained by changing the dict; // address pointer bool flag = false; // indicates whether the dictionary contains the word [I] int Len = strlen (word [I]); For (int K = 0; k <dictnum; k ++) // traverse the dictionary {If (dictlen [k] = Len) // change or equal {If (! Strcmp (word [I], dict [k]) {flag = true; break;} else if (Change (word [I], dict [k]) address [PA ++] = K;} else if (LEN-dictlen [k] = 1) // Delete {If (DEL (word [I], dict [k]) Address [PA ++] = K;} else if (dictlen [k]-len = 1) // Add {If (add (word [I], dict [k]) Address [PA ++] = K ;}}/* output */If (FLAG) cout <word [I] <"is correct" <Endl; else {cout <word [I] <":"; for (Int J = 0; j <Pa; j ++) cout <dict [Address [J] <''; cout <Endl;} Delete address;} r Eturn 0;} void input (void) {While (CIN> dict [dictnum] & dict [dictnum ++] [0]! = '#'); While (CIN> word [wordnum] & word [wordnum ++] [0]! = '#'); Dictnum --; // remove '#' wordnum --; return;} bool change (char * word, char * dict) // wordlen = dictlen {int DIF = 0; // records the number of different characters in the same position of word and dict while (* word) {If (* (word ++ )! = * (Dict ++) {DIF ++; If (DIF> 1) return false ;}} return true ;} bool del (char * word, char * dict) // wordlen = dictlen + 1 {int DIF = 0; // records the number of different characters in the corresponding position of word and dict while (* Word) {If (* word! = * Dict) {word ++; // after the word is moved one bit, it matches DIF ++; If (DIF> 1) return false;} else {word ++; dict ++; }}return true;} bool add (char * word, char * dict) // wordlen = DictLen-1 {int DIF = 0; // record the number of different characters in the corresponding position of word and dict while (* dict) {If (* word! = * Dict) {dict ++; // After the dict moves one bit, it matches DIF ++; If (DIF> 1) return false;} else {word ++; dict ++ ;}} return true ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.