POJ1035 ZOJ2040 Spell checker

//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
#
请按任意键继续. . .
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值