Poj 1035 -- spell checker

Source: Internet
Author: User
Spell checker
Time limit:2000 ms   Memory limit:65536 K
Total submissions:18712   Accepted:6856

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

iishashavebemymorecontestmetooifaward#meawaremcontesthavooorifimre#

Sample output

Me is correctaware: awardm: I my mecontest is correcthav: Has haveoo: tooor: I is correctfi: Imre: More me

The question of the string, give a dictionary, and then judge whether the entered word is in the dictionary. If not, find the word in the length minus one/plus one/exactly/and satisfy only one letter,
If you use LSC for the first time, you may not be able to perform this operation in the correct posture. If the request times out, the system will be able to perform this operation directly.

  1 /*======================================================================  2  *           Author :   kevin  3  *         Filename :   SpellChecker1.cpp  4  *       Creat time :   2014-07-19 13:09  5  *      Description :  6 ========================================================================*/  7 #include <iostream>  8 #include <algorithm>  9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 10005 15 using namespace std; 16 char words[M+10][20]; 17 int cnt; 18 void init() 19 { 20     cnt = 0; 21     while(scanf("%s",words[cnt])){ 22         if(strcmp(words[cnt],"#") == 0){ 23             break; 24         } 25         else cnt++; 26     } 27 } 28 bool judge1(char a[],char b[],int len1,int len2) 29 { 30     int cnt = 0; 31     for(int i = 0; i < len1; i++){ 32         if(a[i] != b[i]) 33             cnt++; 34     } 35     if(cnt == 1) return true; 36     return false; 37 } 38 bool judge2(char a[],char b[],int len1,int len2) 39 { 40     int flag = 0; 41     for(int i = 0; i < len1; i++){ 42         flag = 1; 43         for(int j = 0,k = 0; j < len1 && k < len2; ){ 44             if(j == i){ 45                 j++; continue; 46             } 47             if(a[j] != b[k]){ 48                 flag = 0; 49             } 50             j++; k++; 51         } 52         if(flag) return true; 53     } 54     return false; 55 } 56 bool judge3(char a[],char b[],int len1,int len2) 57 { 58     int flag; 59     for(int i = 0; i < len2; i++){ 60         flag = 1; 61         for(int j = 0,k = 0; j < len1 && k < len2; ){ 62             if(k == i){ 63                 k++; continue; 64             } 65             if(a[j] != b[k]) 66                 flag = 0; 67             j++; k++; 68         } 69         if(flag) return true; 70     } 71     return false; 72 } 73 int main(int argc,char *argv[]) 74 { 75     init(); 76     char str[20]; 77     while(scanf("%s",str)){ 78         if(strcmp(str,"#") == 0){ 79             break; 80         } 81         int flag = 0; 82         for(int i = 0; i < cnt; i++){ 83             if(strcmp(words[i],str) == 0){ 84                 printf("%s is correct\n",str); 85                 flag = 1; 86                 break; 87             } 88         } 89         if(!flag){ 90             printf("%s:",str); 91             int len1 = strlen(str); 92             for(int i = 0; i < cnt; i++){ 93                 int len2 = strlen(words[i]); 94                 if(len1 == len2){ 95                     if(judge1(words[i],str,len2,len1)){ 96                         printf(" %s",words[i]); 97                     } 98                 } 99                 if(len1+1 == len2){100                     if(judge2(words[i],str,len2,len1)){101                         printf(" %s",words[i]);102                     }103                 }104                 if(len1 == len2+1){105                     if(judge3(words[i],str,len2,len1)){106                         printf(" %s",words[i]);107                     }108                 }109             }110             printf("\n");111         }112     }113     return 0;114 }
View code

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.