POJ, 1035 Spell checker(根据单词表,寻找符合相似度要求的单词。)

来源:互联网 发布:数据的标准化处理 编辑:程序博客网 时间:2024/04/30 06:38

Spell checker

From:POJ, 1035

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

iishashavebemymorecontestmetooifaward#meawaremcontesthavooorifimre#

Sample Output

me is correctaware: awardm: i my mecontest is correcthav: has haveoo: tooor:i is correctfi: imre: more me
题意:
输入的第一组为字典,第二组为目标单词,要求为找出与单词相同或者相差一步的单词,并按要求输出。
解题思路:
先判定是否一样,一样直接输出;若不一样,但是长度相同,进行判定;若长度相差一,在进行判定,其他的不考虑。

代码:

#include<iostream>#include<cstring>#include<cstdio>usingnamespace std; charstr1[11000][15],str2[11000][15];int n1,n2; voidsolve(){    for(int i=0;i<n2;i++){        bool f=false;        for(int j=0;j<n1;j++){            if(strcmp(str2[i],str1[j])==0){                printf("%s iscorrect\n",str2[i]);                f=true;            }        }        if(f) continue;        printf("%s:",str2[i]);        for(int j=0;j<n1;j++){           if(strlen(str2[i])==strlen(str1[j])){                for(intk=0;k<strlen(str1[j]);k++){                    if(str1[j][k]!=str2[i][k]){                        char temp[15];                        strcpy(temp,str2[i]);                        temp[k]=str1[j][k];                       if(strcmp(temp,str1[j])==0){                            printf("%s",str1[j]);                        }                    }                }            }            else if((strlen(str2[i])-strlen(str1[j]))==1||(strlen(str2[i])-strlen(str1[j]))==-1){                    char temp1[15],temp2[15];                    strcpy(temp1,str1[j]);                    strcpy(temp2,str2[i]);                   if(strlen(str1[j])<strlen(str2[i])){                        strcpy(temp1,str2[i]);                        strcpy(temp2,str1[j]);                    }                    int k=0,flag=1;                    for(;k<strlen(temp2)&& temp1[k]==temp2[k];k++);                    for(;k<strlen(temp2);k++){                       if(temp1[k+1]!=temp2[k]){                            flag=0;                            break;                        }                    }                    if(flag==1){                        printf("%s",str1[j]);                    }            }        }        printf("\n");    }} int main(){    n1=n2=0;   while(scanf("%s",str1[n1])!=EOF&&strcmp("#",str1[n1])!=0){        n1++;    }   while(scanf("%s",str2[n2])!=EOF&&strcmp("#",str2[n2])!=0){        n2++;    }    solve();    return 0;}


0 0
原创粉丝点击