POJ 1035 Spell checker 字符串 操作

题意:给出字典 #为结束,再 输入单词, 判断该单词的拼写状态,状态有5种:

1. 字典中包含该单词;

2.该单词比正确单词缺了一个字母;

3.该单词与正确单词比 只有一个不同;

4.该单词与正确单词比 多了一个字母;

5.该单词 在字典中 找不到上述情况。

  1: 输出 word is correct;

  2 ~4:按照 字典中的顺序 输出 所有可能的正确单词;

  5:输出原单词; 

思路:暴力 枚举。157MS

#include <iostream>
using namespace std;
char word[10100][20],temp[20];
int sum,len,ans[2000];
void spellcheak()
{
	int i,j,k,dif,lenb,get=0;
	for(i=0; i<sum; i++)
	{
		dif=0;
		lenb = strlen(word[i]);
		if(len==lenb+1)
		{
			for(j=0,k=0; j<len && dif!=2; j++,k++)
			{
				if(temp[k]!=word[i][j])
				{
					dif++; j--;
				}
			}
			if(dif==1)
			{
				ans[get++]=i;
			}
		}
		else if(len==lenb)
		{
			for(j=0; j<lenb; j++)
			{
				if(temp[j]!=word[i][j])
					dif++;
			}
			if(dif==1)
			{
				ans[get++]=i;
			}
			if(dif==0)
			{
				printf("%s is correct\n",temp);
				return ;
			}
		}
		else if(len==lenb-1)
		{
			for(j=0,k=0; j<lenb && dif!=2; j++,k++)
			{
				if(temp[k]!=word[i][j])
				{
					dif++; k--;
				}
			}
			if(dif==1)
			{
				ans[get++]=i;
			}
		}
	}
	printf("%s:",temp);
	for(i=0; i<get; i++)
	{
		printf(" %s",word[ans[i]]);
	}
	printf("\n");
}
int main()
{
	while(gets(word[sum]) && word[sum++][0]!='#');
	sum--;
	while(gets(temp) && temp[0]!='#')
	{
		len = strlen(temp);
		spellcheak();
	}
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值