poj 1035 Spell checker(字符串)

题意:

           检测输入字符串和库里的字符串是否相符

           相符按要求输出即可,若不符;分三种情况讨论

           1;字符串长度相等  处理很简单,掠过

           2;字符串不等时,分别设两个指针,从前向后,从后向前移动判断即可

             注意判断边界条件 top1>di  !!! 

        

#include<stdio.h>
#include<math.h>
#include<string.h>
struct node
{
char str[20];
}map[10005];

int main()
{
int top=0;
while(gets(map[top++].str)&&map[top-1].str[0]!='#') ;
top--;
char num[20];
while(gets(num)&&num[0]!='#')
{
int flag=0;

for(int i=0;i<top;i++)
{
if(strcmp(map[i].str,num)==0)
{
flag=1;break;
}

//printf("%d\n",strcmp(map[i].str,num));
}
if(flag) printf("%s is correct\n",num);
else
{
printf("%s:",num);
int len=strlen(num);
for(int i=0;i<top;i++)
{
int len1=strlen(map[i].str);
int x=len-len1;
if(fabs(x)>1) continue;
else
{
int flag_1=0;
if(x==0)
{
for(int j=0;num[j]!='\0';j++)
{
if(num[j]!=map[i].str[j])
flag_1++;
if(flag_1>=2) break;
}
if(flag_1==1) printf(" %s",map[i].str);
}
else if(x>0)
{
int di=0,top1=len-1;
while(num[di]==map[i].str[di]&&di<top1) di++;

while(num[top1]==map[i].str[top1-1]&&top1>di) top1--;

if(di==top1) printf(" %s",map[i].str);
}
else
{
int di=0,top1=len1-1;
while(num[di]==map[i].str[di]&&di<top1) di++;

while(num[top1-1]==map[i].str[top1]&&top1>di) top1--;

if(di==top1) printf(" %s",map[i].str);
}

}
}
printf("\n");
}

}

}



Just a little, maybe change the world
原文地址:https://www.cnblogs.com/skyming/p/2380919.html