poj1035 模拟

题意:输入一部字典,然后输入若干单词,
1.若该单词在字典中能查到,则输出correct
2.若该单词通过替换、删除、添加一个字符后,可以在字典中查到,输出字典中单词
3.不符合1、2者,输出空

算法:模拟


#include <iostream>
#include <string.h>
using namespace std;

const int SIZE=10010;
char dict[SIZE][20];
char word[20];
int num = 0;

void solve()
{
	int diff = 0;
	char tmp_word[20];
	int len = strlen(word);
	for (int i=0; i<num; i++)
	{
		if (strcmp(dict[i],word) == 0)
		{
			cout << word << " is correct" << endl;
			return;
		}
	}
	cout << word << ":";
	for (int i=0; i<num; i++)
	{
		int len_dict = strlen(dict[i]);
		// 替换 
		if (len_dict == len)
		{
			diff = 0;
			for (int j=0; j<len; j++)
			{
				if (dict[i][j] != word[j])
				{
					diff++; 
				}
			}
			if (diff == 1)
			{
				cout << " " << dict[i];
			}
		}
		// 删除
		else if (len_dict == len-1) 
		{
			for (int j=0; j<len; j++)
			{
				int p = 0;
				for (int k=0; k<len; k++)
				{
					if (j != k)
					{
						tmp_word[p++] = word[k];
					}
				}
				tmp_word[p] = '\0';
				if (strcmp(tmp_word,dict[i]) == 0)
				{
					cout <<" " << dict[i];
					break;
				}
			}
		}
		// 添加 
		else if (len_dict-1 == len)
		{
			int p=0;
			for (int j=0; j<len_dict; j++)
			{
				int p = 0;
				for (int k=0; k<len_dict; k++)
				{
					if (j != k)
					{
						tmp_word[p++] = dict[i][k];	
					}
				}
				tmp_word[p] = '\0';
				if (strcmp(tmp_word,word) == 0)
				{
					cout << " " << dict[i] ;
					break;
				}
			}
		}
	}
	cout << endl;
}

int main()
{
	while (cin >> dict[num])
	{
		if (dict[num][0] == '#')
		{
			break;
		}
		num++;
	}
	while (cin >> word)
	{
		if (word[0] == '#')
		{
			break;
		}
		solve();
	}
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kangwq2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值