Spell checker(POJ--1035

来源:互联网 发布:怎么删除mac上的文件 编辑:程序博客网 时间:2024/06/08 13:43

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.
题意:不断输入字符串以“#”结束,以上字符串相当于字典里的字符串,接着不断输入字符串,以“#”结束,这时每输入一串字符串去对比字典里的字符串,如果是字典里的字符串则输出“%s is correct”;如果只是与字典里的字符串有一个字母不同或少一个字母或多一个字母则按要求输出原字符串和字典里的字符串;如果都不符合则按要求只输出原字符串。
思路:由于数据不是很多,最多才10000*50*15,所以直接暴力即可。

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 <cstdio>#include <iostream>#include <queue>#include <cstring>using namespace std;char dic[10005][20],cheack[20];int main(){    //freopen("oo.text","r",stdin);    memset(dic,0,sizeof(dic));    memset(cheack,0,sizeof(cheack));    int tp=0;    while(1)    {        scanf("%s",dic[tp]);            //将字典里的字符串记录下来        if(dic[tp][0]=='#')                //以“#”号结束            break;        tp++;    }    while(~scanf("%s",cheack))    {        if(cheack[0]=='#')            break;        int flag=0;        for(int i=0; i<tp; i++)                  //先遍历一遍看是否在字典里        {            if(strcmp(dic[i],cheack)==0)                flag=1;        }        if(flag)            printf("%s is correct\n",cheack);        else                                           //不在字典里的情况        {             int len1,len2,cnt,j,k;            len1=strlen(cheack);            printf("%s:",cheack);            for(int i=0; i<tp; i++)            {                len2=strlen(dic[i]);                if(len1==len2)                {                    cnt=0;                    for(j=0; j<len1; j++)                    {                        if(dic[i][j]!=cheack[j])                            cnt++;                    }                    if(cnt==1)                        //长度相同只是其中一个字母不同则符合要求                        printf(" %s",dic[i]);                }                else if(len1==len2-1)                {                    cnt=0;                    j=0;                    k=0;                    while(1)                    {                        if(dic[i][j]==cheack[k])                        {                            j++;                            k++;                            cnt++;                        }                        else                        {                            j++;                        }                        if(j==len2||k==len1)                            break;                    }                    if(cnt==len2-1)               //长度少一,但其是字典中某字符串的子串符合要求                        printf(" %s",dic[i]);                }                else if(len1==len2+1)                {                    cnt=0;                    j=0;                    k=0;                    while(1)                    {                        if(dic[i][j]==cheack[k])                        {                            j++;                            k++;                            cnt++;                        }                        else                        {                            k++;                        }                        if(j==len2||k==len1)                            break;                    }                    if(cnt==len1-1)               //长度多一即比字典中某字符串多一个字母符合要求                        printf(" %s",dic[i]);                }            }            printf("\n");        }    }    return 0;}<strong></strong>


0 0
原创粉丝点击