Automatic Correction of Misspellings

来源:互联网 发布:网络淘宝兼职怎么赚钱 编辑:程序博客网 时间:2024/04/29 03:11

Problem Description
Some text editors offer a feature to correct words which seem to be written incorrectly. In this problem you are asked to implement a simple Automatic Correction of Misspellings (ACM).

ACM takes care of the following misspellings of words:

1.One letter is missing (e.g., letter is written leter) or too much (e.g., letter is written lettter).
2.One letter is wrong (e.g., letter is written ketter)
3.The order of two adjacent letters is wrong (e.g., letter is written lettre)

ACM is based on a dictionary of known words. When a text contains a word which is not in the dictionary, ACM will try to replace it by a similar word of the dictionary. Two words are similar if we can transform one word into the other by doing exactly one of the misspellings listed above. An unknown word is left unchanged if there is no similar word in the dictionary.
 

Input
The first line of the input file will give the number n of words in the dictionary (n ≤ 10000). The next n lines contain the dictionary words. The following line contains an integer q ≤ 1000, the number of query words. The next q lines contain the query words. You may assume that each word in the input consists of 1 to 25 lower case letters ('a' to 'z').
 

Output
For each query word, print one line with the query word followed by one of the following possibilities:

1.is correct, if the word occurs in the dictionary.
2.is a misspelling of <x>, where <x> is a word of the dictionary similar to the query word, and the query word is not in the dictionary. In the case that there are several possibilities, select the word from the dictionary which appeared earlier in the input.
3.is unknown, if cases 1 and 2 do not apply.
 

Sample Input
10thisisadictionarythatwewilluseforus6suasthedictonaryuswilll
 

Sample Output
su is a misspelling of usas is a misspelling of isthe is unknowndictonary is a misspelling of dictionaryus is correctwilll is a misspelling of will
#include<iostream>#include<stack>#include<algorithm>#include<string.h>#include<math.h>#include<stdio.h>using namespace std;struct node{    char str[30];    int len;}N[10005];int flag,slen,n,f;char ss[30];void First(){    int k,stk[26],top,i,j;    for(i=0;flag&&i<n;i++)    {        if(N[i].len == slen)        {             k = 0;            top = 0;            for(j=0;k<=2 && j<slen;j++)//判断是否全部正确            {                if(N[i].str[j] != ss[j])                {                    k++;                    stk[top++] = j;                }            }            if(k == 0)//全部正确            {                flag = 0;                f = i;                break;            }            else if(k == 1 && flag > 1)//只有一位不一样            {                flag = 1;                f = i;            }            else if(k == 2 && flag > 1)//相邻两个不同            {                if( N[i].str[stk[0]] == ss[stk[1]] && N[i].str[stk[1]] == ss[stk[0]])                {                    flag = 1;                    f = i;                }            }        }       else if(N[i].len - slen == 1 && flag > 1)//ss少一位       {           bool judge = false;           for(j=0,k=0;j<N[i].len;j++)           {               if(N[i].str[j] == ss[k])                k++;               else if(!judge)                judge = true;               else                break;           }           if(k >= N[i].len)           {               flag = 1;               f = i;           }       }       else if(slen - N[i].len == 1 && flag > 1)//ss多一位       {           bool judge = false;           int j;           for(j=0,k=0;j<slen;j++)           {               if(N[i].str[k] == ss[j])                k++;               else if(!judge)                judge = true;               else                break;           }           if(j >= slen)           {               flag = 1;               f = i;           }       }    }}int main(){    while(cin>>n)    {        for(int i=0;i<n;i++)        {            cin>>N[i].str;            N[i].len = strlen(N[i].str);        }        int m;        cin>>m;        while(m--)        {            cin>>ss;            slen = strlen(ss);            flag = 2;            First();            if(flag == 0)            {                cout<<ss<<" is correct"<<endl;            }            else if(flag == 1)            {                cout<<ss<<" is a misspelling of "<<N[f].str<<endl;            }            else if(flag == 2)            {                cout<<ss<<" is unknown"<<endl;            }        }    }    return 0;}


0 0
原创粉丝点击