//6_3_1: Spell checker 检查最相近的单词并输出 POJ1035 ZOJ2040
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char word[10000][16];
int match(char str1[],int len,char str2[])
{
int i,pos1,pos2,flag = 0;
for(i = 0;i < len + 1;i ++)
{
pos1 = pos2 = 0;
while(pos2 < len)
{
if(pos1 == i) pos1++;
if(str1[pos1] == str2[pos2])
{
pos1++;
pos2++;
}
else break;
}
if(pos2 == len)
{
flag = 1;
break;
}
}
if(flag == 1) return 1;
else return 0;
}
int main()
{
int i,j,len1,len2,flag,word_cnt = 0,wrong_cnt;
char str[16];
while(scanf("%s",word[word_cnt]),word[word_cnt][0] != '#') word_cnt ++;
while(scanf("%s",str),str[0] != '#')
{
flag = 0;
for(i = 0;i < word_cnt;i ++)
if(strcmp(str,word[i]) == 0)
{
flag = 1;
break;
}
if(flag) printf("%s is correct\n",str);
else
{
printf("%s:",str);
len1 = strlen(str);
for(i = 0;i < word_cnt;i ++)
{
len2 = strlen(word[i]);
if(len1 == len2)
{
j = wrong_cnt = 0;
while(str[j])
{
if(str[j] == word[i][j]) j ++;
else
{
wrong_cnt++;
j ++;
if(wrong_cnt == 2) break;
}
}
if(wrong_cnt == 1) printf(" %s",word[i]);
}
else if(len1 + 1 == len2)
{
if(match(word[i],len1,str)) printf(" %s",word[i]);
}
else if(len2 + 1 == len1)
{
if(match(str,len2,word[i])) printf(" %s",word[i]);
}
}
printf("\n");
}
}
return 0;
}
/*测试结果:通过POJ1035检测 ZOJ输入格式不同,需要稍作修改
i
is
has
have
be
my
more
contest
me
too
if
award
#
me
me is correct
aware
aware: award
m
m: i my me
contest
contest is correct
hav
hav: has have
oo
oo: too
or
or:
i
i is correct
fi
fi: i
mre
mre: more me
#
请按任意键继续. . .
*/
POJ1035 ZOJ2040 Spell checker
最新推荐文章于 2017-04-20 15:48:20 发布