poj 1035 Spell checker

Spell checker

Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 17495
Accepted: 6421

Description

You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their forms.
If the word is absent in the dictionary then it can be replaced by correct words (from the dictionary) that can be obtained by one of the following operations:
?deleting of one letter from the word;
?replacing of one letter in the word with an arbitrary letter;
?inserting of one arbitrary letter into the word.
Your task is to write the program that will find all possible replacements from the dictionary for every given word.

Input

The first part of the input file contains all words from the dictionary. Each word occupies its own line. This part is finished by the single character '#' on a separate line. All words are different. There will be at most 10000 words in the dictionary.
The next part of the file contains all words that are to be checked. Each word occupies its own line. This part is also finished by the single character '#' on a separate line. There will be at most 50 words that are to be checked.
All words in the input file (words from the dictionary and words to be checked) consist only of small alphabetic characters and each one contains 15 characters at most.

Output

Write to the output file exactly one line for every checked word in the order of their appearance in the second part of the input file. If the word is correct (i.e. it exists in the dictionary) write the message: " is correct". If the word is not correct then write this word first, then write the character ':' (colon), and after a single space write all its possible replacements, separated by spaces. The replacements should be written in the order of their appearance in the dictionary (in the first part of the input file). If there are no replacements for this word then the line feed should immediately follow the colon.

Sample Input

i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#

Sample Output

me is correct
aware: award
m: i my me
contest is correct
hav: has have
oo: too
or:
i is correct
fi: i
mre: more me

Source

Northeastern Europe 1998

拼写检查(中文版)

  • 查看
  • 提交
  • 统计
  • 提问
总时间限制:2000ms内存限制:65536kB描述
现在有一些英语单词需要做拼写检查,你的工具是一本词典。需要检查的单词,有的是词典中的单词,有的与词典中的单词相似,你的任务是发现这两种情况。单词A与单词B相似的情况有三种:
1、删除单词A的一个字母后得到单词B;
2、用任意一个字母替换单词A的一个字母后得到单词B;
3、在单词A的任意位置增加一个字母后得到单词B。
你的任务是发现词典中与给定单词相同或相似的单词。


输入第一部分是词典中的单词,从第一行开始每行一个单词,以"#"结束。词典中的单词保证不重复,最多有10000个。
第二部分是需要查询的单词,每行一个,以"#"结束。最多有50个需要查询的单词。
词典中的单词和需要查询的单词均由小写字母组成,最多包含15个字符。输出按照输入的顺序,为每个需要检查的单词输出一行。如果需要检查的单词出现在词典中,输出“?x is correct",?x代表需要检查的单词。如果需要检查的单词没有出现在词典中,则输出"?x: ?x1 ?x2 ...?xn",其中?x代表需要检查的单词,?x1...?xn代表词典中与需要检查的单词相似的单词,这些单词中间以空格隔开。如果没有相似的单词, 输出"?x:"即可。样例输入
i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#
样例输出
me is correct
aware: award
m: i my me
contest is correct
hav: has have
oo: too
or:
i is correct
fi: i
mre: more me

解答:

/*=============================================================================
#     FileName: spell.cpp
#         Desc: poj 1035
#       Author: zhuting
#        Email: [email protected]
#     HomePage: my.oschina.net/locusxt
#      Version: 0.0.1
#    CreatTime: 2013-12-04 15:44:51
#   LastChange: 2013-12-04 19:35:52
#      History:
=============================================================================*/

/*
 * 数据在http://poj.org/showmessage?message_id=172686
 */
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#define max_len 16
#define maxn 10005
using namespace std;

class word
{
public:
	char c[max_len];
	int len;
};
word w[maxn];/*字典类*/


/*
 * 一开始以为要分情况讨论的.....
 * 找到一样的state置4
 * 找到修改的置3
 * 找到短的置2
 * 找到长的置1
 * 找不到置0
 */

void find(char tof[], int len, int dic_n, int &state)/*tof为要找的字符串,state为4时已经找到完全匹配的字符串*/
{
	char* cur = NULL;/*没用*/
	int cur_num = 0;
	vector <char*> vc;/*记录相似字符串的指针*/

	for (int i = 0; i < dic_n; ++i)
	{
		if (state == 4) break;
		int delta = w[i].len - len;
		switch (delta)
		{
		case 0:
		{
			if (state == 4) break;
			int error_num = 0;/*失配的次数*/
			for (int j = 0; j < len; ++j)
			{
				if (error_num >= 2) break;
				if (w[i].c[j] != tof[j]) ++error_num;
			}
			if (error_num == 0)
			{
				cur = w[i].c;
				state = 4;
			}
			else if (error_num == 1)
			{
				vc.push_back(w[i].c);
				state = 3;
			}
			break;
		}
		case 1:
		{
			if (state == 4) break;
			int j = 0, k = 0;
			bool is_add = 0;/*记录是否已经失配过*/
			while (j < len + 1 && k < len)
			{
				if (w[i].c[j] == tof[k])
				{
					++j;
					++k;
				}
				else
				{
					if (is_add) break;
					++j;
					is_add = 1;
				}
			}
			if (k == len)/*这里的边界条件很重要*/
			{
				vc.push_back(w[i].c);
				state = 1;
			}
			break;
		}
		case -1:
		{
			if (state == 4) break;
			int j = 0, k = 0;
			bool is_add = 0;
			while (j < len - 1 && k < len)
			{
				if (w[i].c[j] == tof[k])
				{
					++j;
					++k;
				}
				else
				{
					if (is_add) break;
					++k;
					is_add = 1;
				}
			}
			if (j == len - 1)/*重要*/
			{
				vc.push_back(w[i].c);
				state = 2;
			}
			break;
		}
		default:
			break;
		}
	}
	if (state == 4)
	{
		printf("%s is correct\n", tof);
		return;
	}
	printf("%s:", tof);
	for (int i = 0; i < vc.size(); ++i)
	{
		printf(" %s", vc[i]);
	}
	printf("\n");
	return;

}


int main()
{
	char ch_tmp[max_len];
	int dic_num = 0;

	while (true)
	{
		scanf("%s", ch_tmp);
		if (ch_tmp[0] != '#')
		{
			w[dic_num].len = strlen(ch_tmp);
			strcpy(w[dic_num++].c, ch_tmp);
		}
		else break;
	}

	while (true)
	{
		scanf("%s", ch_tmp);
		if (ch_tmp[0] == '#') break;
		int tmp_len = strlen(ch_tmp);
		int state = 0;
		find(ch_tmp, tmp_len, dic_num, state);
	}
	return 0;
}







你可能感兴趣的:(poj)