poj 1035 Spell checker

ACM比赛整理

共 3535字,需浏览 8分钟

 ·

2021-11-18 08:28

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 个单词需要检查。
输入文件中的所有单词(字典中的单词和要检查的单词)仅由小字母组成,每个最多包含 15 个字符。

输出

对于每个选中的单词,按照它们在输入文件的第二部分中出现的顺序,将它们精确地写入输出文件一行。如果这个词是正确的(即它存在于字典中)写下信息:“ 是正确的”。如果单词不正确,则先写这个单词,然后写字符':'(冒号),在一个空格后写下所有可能的替换,用空格分隔。替换应按顺序写入它们在字典中的出现(在输入文件的第一部分)。如果这个词没有替换,那么换行符应该紧跟在冒号之后。

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



题意就是给你一个字典,再给你一些字符,首先如果字典中有这个字符串,则直接输出,如果没有的话,那就找字符串加一个字符或少一个字符或者换一个字符是否可以在字典中找到相应的字符串

 

解题思路:我是用string类型的,比较方便看两个字符串是否相等,用char的话,就是strcmp函数也行。

如果找不到相等的,那么久分别在字典中找到与这个字符串的长度相差1的或者相等的。

然后匹配,如果匹配的结果相差一个则输出



代码:

#include 
#include
#include
#include
#include

using namespace std;

string str[10005],str1[10005];

int main()
{
int dic=0,need=0;
while(cin>>str[dic])
{
if(str[dic]=="#")
break;
dic++;
}
while(cin>>str1[need])
{
if(str1[need]=="#")
break;
need++;
}
for(int i=0;i {
int flog=0; //标记,如果找得到相同的字符串,则continue。
for(int j=0;j {
if(str1[i]==str[j])
{
cout< flog=1;
break;
}
}
if(flog==1)
continue;
int len=str1[i].size();
cout< for(int j=0;j {
int strl=str[j].size();
if(strl==len||strl==len+1||strl==len-1)
{ //字符串相差1的或者相等的,就用来匹配是否有可能相差一个字符,这是一种减枝的办法。
int ans=0;
if(len>strl)
{ //吧那个较长的字符作为被匹配的,用短的来匹配长的字符串。
for(int m=0,d=0;m {
if(str1[i][m]==str[j][d])
{
ans++;
d++;
}
}
}
else if(len {
for(int m=0,d=0;m {
if(str[j][m]==str1[i][d])
{
ans++;
d++;
}
}
}
else if(len==strl)
{
for(int m=0,d=0;m if(str[j][m]==str1[i][d])
ans++;
}
if(len>=strl&&ans==len-1)
cout<<" "< if(len cout<<" "< }
}
cout< }
return 0;
}


浏览 59
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报