您的位置:首页 > 其它

POJ 1035 Spell checker(字符串)

2012-12-13 11:31 691 查看
题目链接

考虑各种情况,然后注意敲的稳一点,忘考虑一种情况,2Y.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <map>
using namespace std;
char word[10001][21];
char s[21];
int len[10001];
int Abs(int x)
{
if(x < 0)
return -x;
else
return x;
}
int main()
{
int i,j,n,k1,k,L,z;
map<string,int>mp;
for(i = 1;;i ++)
{
scanf("%s",word[i]);
len[i] = strlen(word[i]);
if(word[i][0] == '#')
break;
mp[word[i]] = 1;
}
n = i-1;
for(i = 1;;i ++)
{
scanf("%s",s);
if(s[0] == '#') break;
L = strlen(s);
if(mp[s])
{
printf("%s is correct\n",s);
continue;
}
printf("%s:",s);
for(j = 1;j <= n;j ++)
{
if((len[j]-L) > 1)
continue;
if(len[j] == L)
{
z = 0;
for(k = 0;k < L;k ++)
{
if(s[k] != word[j][k])
z ++;
if(z >= 2)
break;
}
if(z == 1)
printf(" %s",word[j]);
}
else if(len[j] > L)
{
z = 0;
for(k = 0,k1 = 0;k < L;k ++,k1 ++)
{
if(s[k] != word[j][k1])
{
if(s[k] == word[j][k1+1])
k1 ++;
else
break;
z ++;
}
}
if(k == L&&k1 == len[j]&&z == 1)
printf(" %s",word[j]);
else if(k == L&&k1 == len[j]-1&&z == 0)
printf(" %s",word[j]);
}
else
{
z = 0;
for(k = 0,k1 = 0;k < L;k ++,k1 ++)
{
if(s[k] != word[j][k1])
{
if(s[k+1] == word[j][k1])
k ++;
else
break;
z ++;
}
}
if(k == L&&k1 == len[j]&&z == 1)
printf(" %s",word[j]);
else if(k == L+1&&k1 == len[j]+1&&z == 1)//随便试了一组,这里就查出错了。。aaa aaaa
printf(" %s",word[j]);
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: