POJ 1035 Spell checker 简单字符串匹配

在输入的单词中删除或替换或插入一个字符,看是否在字典中。直接暴力,172ms。。

 #include <stdio.h>
#include <string.h>
int len[];
char dic[][], s[]; bool del(char s1[], char s2[])
{
bool isdel = ;
for(int i = , j = ; s1[i] && s2[j]; i++, j++)
{
if(s1[i] != s2[j])
{
if(isdel)
return ;
j--;
isdel = ;
}
}
return ;
} bool rep(char s1[], char s2[])
{
bool isrep = ;
for(int i = , j = ; s1[i] && s2[j]; i++, j++)
{
if(s1[i] != s2[j])
{
if(isrep)
return ;
isrep = ;
}
}
return ;
} bool ins(char s1[], char s2[])
{
bool isins = ;
for(int i = , j = ; s1[i] && s2[j]; i++, j++)
{
if(s1[i] != s2[j])
{
if(isins)
return ;
i--;
isins = ;
}
}
return ;
} int main()
{
int cnt = ;
while(scanf("%s", dic[cnt]) != EOF)
{
if(dic[cnt][] == '#')break;
len[cnt] = strlen(dic[cnt++]);
}
while(scanf("%s", s) != EOF)
{
if(s[] == '#')break;
bool ok = ;
for(int i = ; i < cnt; i++)
{
if(strcmp(s, dic[i]) == )
{
ok = ;
printf("%s is correct", s);
}
}
if(!ok)
{
printf("%s:", s);
int len_s = strlen(s);
for(int i = ; i < cnt; i++)
{
if(len_s - len[i] == && del(s, dic[i]))
printf(" %s", dic[i]);
if(len_s - len[i] == && rep(s, dic[i]))
printf(" %s", dic[i]);
if(len_s - len[i] == - && ins(s, dic[i]))
printf(" %s", dic[i]);
}
}
printf("\n");
}
return ;
}
上一篇:STL源码剖析之_allocate函数


下一篇:导出客户端对象时出现“java.rmi.ConnectException:Connection拒绝托管”