POJ 1035 Spell checker(字典)

题目:

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

这个题目就是把10000个字符串和50个字符串一一比较。

因为在不是correct的情况下,比较得到一个相似的就直接输出,不用保存下来,所以在这之前先要判断到底是不是correct的情况。

直接从头到尾遍历一遍看有没有相同的字符串就可以了。这个过程完全不会影响算法的复杂度。

然后在比较2个字符串是不是相似(只隔了一个字母)的时候,先统计长度。

因为在循环里面是必须用到长度的,而且根据长度的情况分4类,分别处理,是非常方便的。

如果把字符串s2去掉任意一个字母,再和s1去比较,因为去掉的字母的位置是任意的,所以会有大量的重复工作。

虽然说一个单词最多只有15个字母,但是字符串去掉一个字母也需要时间,所以这样的方法时间代价还是比较高的。

所以我用的是一个个字符的比较。

其中有个很容易错的地方

if (l1 - l2 == -1)
	{
		for (int i = 0, j = 0; i < l1; i++, j++)
		{
			if (s1[i] != s2[j])
			{
				i--;
				if ((j - i)>1)return false;
			}
		}
		return true;
	}
第7行是 i-- 而不是 j++,否则的话,就会误判 "abcdefg" "abchiefg" 为相似字符串,因为会跳过d和i的比较。

代码:

#include
#include
using namespace std;

bool ok(string s1, string s2)
{
	int l1 = s1.length();
	int l2 = s2.length();
	if (l1 - l2 == -1)
	{
		for (int i = 0, j = 0; i < l1; i++, j++)
		{
			if (s1[i] != s2[j])
			{
				i--;
				if ((j - i)>1)return false;
			}
		}
		return true;
	}
	if (l1 - l2 == 0)
	{
		int t = 0;
		for (int i = 0, j = 0; i < l1; i++, j++)
		{
			if (s1[i] != s2[j])
			{
				if (t)return false;
				t++;
			}
		}
		return true;
	}
	if (l1 - l2 == 1)
	{
		for (int i = 0, j = 0; j < l2; i++, j++)
		{
			if (s1[i] != s2[j])
			{
				j--;
				if ((i - j)>1)return false;
			}
		}
		return true;
	}
	return false;
}

int main()
{
	string s1[10001];
	string s2[51];
	int l1, l2;
	for (l1 = 0;; l1++)
	{
		getline(cin, s1[l1]);
		if (s1[l1] == "#")break;
	}
	for (l2 = 0;; l2++)
	{
		getline(cin, s2[l2]);
		if (s2[l2] == "#")break;
	}
	for (int i = 0; i < 51; i++)
	{
		if (s2[i] == "#")break;
		bool flag = false;
		for (int j = 0; j < 10000; j++)
		{
			if (s1[j] == "#")break;
			if (s1[j] == s2[i])
			{
				flag = true;
				break;
			}
		}
		cout << s2[i];
		if (flag)cout << " is correct";
		else
		{
			cout << ":";
			for (int j = 0; j < 10000; j++)
			{
				if (s1[j] == "#")break;
				if (ok(s1[j], s2[i]))cout << " " << s1[j];
			}
		}
		cout << endl;
	}
	return 0;
}

你可能感兴趣的:(POJ 1035 Spell checker(字典))