Spell checker
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23193 Accepted: 8435
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
i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#
Sample Output
me is correct
aware: award
m: i my me
contest is correct
hav: has have
oo: too
or:
i is correct
fi: i
mre: more me
题意:
给一堆单词,然后查询一堆单词,有:
如果查询的单词被找到了,输出该单词 is coeerct
如果该单词删掉一个字母或者怎么一个字母或者更改一个字母与单词表的某一个单词一样,输出该单词: 找到的单词
除了以上两种情况输出该单词:
题解:
考虑如果查找的单词和单词表一个个匹配,如果匹配到某一个单词时两个单词的字母相差大于1,则肯定不能找到。如果等于1,我们把这两个串dp一下求出最大公共子序列。判断这两个串通过变换能不能得到。如果字母数之差为0,则遍历匹配一下这两个串当他们有且只有一个位置字母不同时。是可以替换得到的。或者他们完全一样。再或者是不一样的。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define f(i,a,b) for(int i = a;i<=b;i++)
#define fi(i,a,b) for(int i = a;i>=b;i--)
using namespace std;
char G[10010][20];
char a[30];
int pp[10010];
int len[10010];
int dp[20][20];
int Dp(char* s1,char* s2){
memset(dp,0,sizeof(dp));
int i,j;
for(i = 0;s1[i]!='\0';i++)
for(j = 0;s2[j]!='\0';j++)
if(s1[i] == s2[j])
dp[i+1][j+1] = dp[i][j] + 1;
else
dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1]);
int tem1 = abs(i-dp[i][j]);
int tem2 = abs(j-dp[i][j]);
if(tem1 == 0&&tem2 == 1){
return 1;
}else if(tem1 == 1&&tem2 == 0){
return 1;
}else if(tem1==1&&tem2==1){
return 1;
}else if(tem1==0&&tem2==0){
return 0;
}
return -1;
}
int main()
{
// freopen("data.in","r",stdin);
int cnt = 0;
while(1){
scanf("%s",G[cnt++]);
len[cnt-1] = strlen(G[cnt-1]);
getchar();
if(G[cnt-1][0] == '#')
break;
}
int n = cnt-1;
while(1){
scanf("%s",a);
if(a[0] == '#')
break;
int ok = 0;
int ok1 = 0;
int i,j;
for(i = 0;i<n;i++){
int len3 = strlen(a);
int tem = abs(len3-len[i]);
if(tem>1)
pp[i] = -1;
else if(tem == 1)
pp[i] = Dp(G[i],a);
else if(tem == 0){
int sum = 0;
for(j = 0;G[i][j]!='\0';j++){
if(G[i][j]!=a[j])
sum++;
if(sum>1){
pp[i] = -1;
break;
}
}
if(sum == 1)
pp[i] = 1;
else if(sum==0)pp[i]=0;
}
if(pp[i] == 0){
break;
}else if(pp[i] == 1)
ok = 2;
}
if(i<n) printf("%s is correct\n",a);
else if(ok == 2){
printf("%s:",a);
f(i,0,n-1)
if(pp[i]==1)
printf(" %s",G[i]);
printf("\n");
}else{
printf("%s:\n",a);
}
}
return 0;
}