Automatic Correction of Misspellings
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1991 | Accepted: 608 |
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:
- One letter is missing (e.g., letter is written leter) or too much (e.g., letter is written lettter).
- One letter is wrong (e.g., letter is written ketter)
- 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 nextq 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:
- is correct, if the word occurs in the dictionary.
- 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.
- is unknown, if cases 1 and 2 do not apply.
Sample Input
10
this
is
a
dictionary
that
we
will
use
for
us
6
su
as
the
dictonary
us
willl
Sample Output
su is a misspelling of us
as is a misspelling of is
the is unknown
dictonary is a misspelling of dictionary
us is correct
willl is a misspelling of will
Source
解题报告:现在是凌晨一点了,也算是昨天内部比赛的题,这次也是我负责的前三个,这是第一个题,其实只要注意细节,读懂题意就行,挺简单的,就是简单的字符串处理,题意就是:先给我们有限个单词作为词库,再给我们若干的单词来判断1.所给但是是否正确(若和词库中的一个完全一样就正确),2、两个字符长度相同只有一个字符不同时或字符长度相同但是相邻的两个字符交换了,3.两个字符长度相差一,其他字符相同即缺一个字符,或多一个字符;4.其他情况下词库中不存在
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 10010;
struct node
{
char word[110];
int lenword;
}dic[MAX];
char qu[1010];
int Judge(char *a, char *b, int len)//长度相差一的时候,a的长度长
{
int i= 0, count = 0, j = 0;
while (i < len && j < len - 1)
{
if (a[i] == b[j])
{
i ++;
j ++;
}
else
{
count ++;
i ++;
}
}
if (j == len - 1 && count == 0)//若前面的都一样只是a最后多一个字符
{
return 1;
}
if (count == 1)//只是一个字符不一样
{
return 1;
}
return 0;
}
int main()
{
int n, i, m, j, lenqu, flag, k, count;
scanf("%d", &n);
for (i = 0; i < n; ++i)
{
scanf("%s", dic[i].word);
dic[i].lenword = strlen(dic[i].word);
}
scanf("%d", &m);
for (i = 0; i < m; ++i)
{
flag = 0;
scanf("%s", qu);
lenqu = strlen(qu);
for (j = 0; j < n; ++j)//和存储的比对
{
if (strcmp (dic[j].word, qu) == 0)//相同时
{
flag = 1;
printf("%s is correct\n", qu);
break;
}
}
if (!flag)
{
for (j = 0; j < n; ++j)
{
if (dic[j].lenword == lenqu)//长度相同时
{
int com[110] = {0};
count = 0;
for (k = 0; k < lenqu; ++k)
{
if (dic[j].word[k] != qu[k])
{
count ++;
com[count] = k;
}
}
if (count == 1)//长度相等时且只有一个字符不一样
{
flag = 1;
}
if (count == 2)//只有两个字符不相同时
{
if (dic[j].word[com[1]] == qu[com[2]] && dic[j].word[com[2]] == qu[com[1]] && com[2] - com[1] == 1)//相邻的颠倒时
{
flag = 1;
}
}
}
else if (dic[j].lenword - 1 == lenqu)//长度相差一时
{
flag = Judge(dic[j].word, qu, dic[j].lenword);
}
else if (dic[j].lenword + 1 == lenqu)//长度相差一时
{
flag = Judge(qu, dic[j].word, lenqu);
}
if (flag)
{
printf("%s is a misspelling of %s\n", qu, dic[j].word);
break;
}
}
}
if (!flag)//不存在时
{
printf("%s is unknown\n", qu);
}
}
return 0;
}