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();
	}
}







你可能感兴趣的:(模拟,poj)