POJ 1035: Spell checker

题目来源:http://poj.org/problem?id=1035

Spell checker

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 27099

Accepted: 9915

Description

You, as a member of a development team for a newspell checking program, are to write a module that will check the correctnessof given words using a known dictionary of all correct words in all theirforms. 
If the word is absent in the dictionary then it can be replaced by correctwords (from the dictionary) that can be obtained by one of the followingoperations: 
?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 fromthe dictionary for every given word. 

Input

The first part of the input file contains allwords from the dictionary. Each word occupies its own line. This part isfinished by the single character '#' on a separate line. All words aredifferent. 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 wordoccupies 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 bechecked. 
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 charactersat most. 

Output

Write to the output file exactly one line forevery checked word in the order of their appearance in the second part of theinput file. If the word is correct (i.e. it exists in the dictionary) write themessage: " is correct". If the word is notcorrect then write this word first, then write the character ':' (colon), andafter a single space write all its possible replacements, separated by spaces.The replacements should be written in the order of their appearance in thedictionary (in the first part of the input file). If there are no replacementsfor 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
#

SampleOutput

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

-----------------------------------------------------

解题思路

对于需要判断的单词,与词典中的单词顺序比对即可。只有与待判断的单词长度相等或长度之差绝对值等于1的词典中的单词才需要判断。

2个注意点:

1. 词典词与待判断单词长度相差1时,通过同时遍历两个单词统计相同字母个数的方式判断,不要用substr方法,会超时

k = 0;
j = 0;
while (j<len && k<diclen)
{
	if (test[j]==dics[k])					// 如果相等,短的单词的iterator++
	{
		cnt++;
		j++;
	}
	k++;									// 不管相不相等,长的单词的iterator都要++
}
if (cnt==len)
{
	match.push_back(dic[i]);
}

2. 如果用substr方法判断,较长单词可能有多种删除方式可以得到较短单词,因此第一次得到较短单词后就要break,否则会重复

-----------------------------------------------------

代码 

#include<iostream>
#include<fstream>
#include<string>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;

#define ONLINE_JUDGE

const int DIC_LEN = 10005;
char dic[DIC_LEN][17] = {};

int main()
{
#ifndef ONLINE_JUDGE
	ifstream fin("1035.txt");
	string s;
	deque<string> dic;
	deque<string>::iterator it, it1;
	deque<string> match;
	int len = 0, diclen = 0, j, cnt;
	bool is_correct = false;
	while (fin >> s)
	{
		if (s=="#")
		{
			break;
		}
		dic.push_back(s);
	}
	while (fin >> s)
	{
		if (s=="#")
		{
			break;
		}
		match.clear();
		is_correct = false;
		len = s.size();
		for (it=dic.begin(); it!=dic.end(); it++)
		{
			cnt = 0;
			string dics = *it;
			diclen = dics.size();
			if (dics==s)
			{
				cout << s << " is correct" << endl;
				is_correct = true;
				break;
			}
			else if (diclen-len==0)
			{
				for (j=0; j<len; j++)
				{
					if (dics.at(j)==s.at(j))
					{
						cnt++;
					}
				}
				if (cnt==len-1)
				{
					match.push_back(dics);
				}
			}
			else if (diclen-len==1)
			{
				for (j=0; j<diclen; j++)
				{
					string d_dics = dics.substr(0,j).append(dics.substr(j+1,diclen));	// 太慢了,会超时
					if (d_dics==s)
					{
						match.push_back(dics);
						break;				// 避免dics可以通过多种删除方式得到s从而把dics存了几遍的情况
					}
				}
			}
			else if (diclen-len==-1)
			{
				for (j=0; j<len; j++)
				{
					string d_s = s.substr(0,j).append(s.substr(j+1,len));
					if (d_s==dics)
					{
						match.push_back(dics);
						break;			// 避免s可以通过多种删除方式得到dics从而把dics存了几遍的情况
					}
				}
			}


		}
		if (!is_correct)
		{
			if (match.size()==0)
			{
				cout << s << ":" << endl;
			}
			else
			{
				cout << s << ":";
				for (it1=match.begin(); it1!=match.end(); it1++)
				{
					cout << " " << *it1;
				}
				cout << endl;
			}
		}
	}
	fin.close();
#endif
#ifdef ONLINE_JUDGE
	deque<char*> match;
	deque<char*>::iterator it1;
	char test[17] = {};
	int len = 0, diclen = 0, i = 0, j = 0, cnt = 0, len_dic = 0, k;
	bool is_correct = false;
	while (scanf("%s",dic[i++]))
	{
		if (dic[i-1][0]=='#')
		{
			break;
		}
	}
	len_dic = i-1;
	while (scanf("%s",test))
	{
		if (test[0]=='#')
		{
			break;
		}
		match.clear();
		is_correct = false;
		len = strlen(test);
		for (i=0; i<len_dic; i++)
		{
			cnt = 0;
			char dics[17];
			strcpy(dics,dic[i]);
			diclen = strlen(dic[i]);
			if (strcmp(dics,test)==0)
			{
				printf("%s is correct\n", test);
				is_correct = true;
				break;
			}
			else if (diclen-len==0)
			{
				for (j=0; j<len; j++)
				{
					if (dics[j]==test[j])
					{
						cnt++;
					}
				}
				if (cnt==len-1)
				{
					match.push_back(dic[i]);
				}
			}
			else if (diclen-len==1)
			{
				cnt = 0;
				k = 0;
				j = 0;
				while (j<len && k<diclen)
				{
					if (test[j]==dics[k])					// 如果相等,短的单词的iterator++
					{
						cnt++;
						j++;
					}
					k++;									// 不管相不相等,长的单词的iterator都要++
				}
				if (cnt==len)
				{
					match.push_back(dic[i]);
				}
			}
			else if (diclen-len==-1)
			{
				cnt = 0;
				k = 0;
				j = 0;
				while (j<diclen && k<len)
				{
					if (test[k]==dics[j])
					{
						cnt++;
						j++;
					}
					k++;
				}
				if (cnt==diclen)
				{
					match.push_back(dic[i]);
				}
			}
		}
		if (!is_correct)
		{
			if (match.size()==0)
			{
				printf("%s:\n",test);
			}
			else
			{
				printf("%s:",test);
				for (it1=match.begin(); it1!=match.end(); it1++)
				{
					printf(" %s",(*it1));
				}
				printf("\n");
			}
		}
	}
	return 0;
#endif
}


猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/80590622