您的位置:首页 > 其它

Spell checker(POJ--1035

2015-08-11 15:50 375 查看
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.
题意:不断输入字符串以“#”结束,以上字符串相当于字典里的字符串,接着不断输入字符串,以“#”结束,这时每输入一串字符串去对比字典里的字符串,如果是字典里的字符串则输出“%s is correct”;如果只是与字典里的字符串有一个字母不同或少一个字母或多一个字母则按要求输出原字符串和字典里的字符串;如果都不符合则按要求只输出原字符串。
思路:由于数据不是很多,最多才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

#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
char dic[10005][20],cheack[20];
int main()
{
//freopen("oo.text","r",stdin);
memset(dic,0,sizeof(dic));
memset(cheack,0,sizeof(cheack));
int tp=0;
while(1)
{
scanf("%s",dic[tp]);            //将字典里的字符串记录下来
if(dic[tp][0]=='#')                //以“#”号结束
break;
tp++;
}
while(~scanf("%s",cheack))
{
if(cheack[0]=='#')
break;
int flag=0;
for(int i=0; i<tp; i++)                  //先遍历一遍看是否在字典里
{
if(strcmp(dic[i],cheack)==0)
flag=1;
}
if(flag)
printf("%s is correct\n",cheack);
else                                           //不在字典里的情况
{
int len1,len2,cnt,j,k;
len1=strlen(cheack);
printf("%s:",cheack);
for(int i=0; i<tp; i++)
{
len2=strlen(dic[i]);
if(len1==len2)
{
cnt=0;
for(j=0; j<len1; j++)
{
if(dic[i][j]!=cheack[j])
cnt++;
}
if(cnt==1)                        //长度相同只是其中一个字母不同则符合要求
printf(" %s",dic[i]);
}
else if(len1==len2-1)
{
cnt=0;
j=0;
k=0;
while(1)
{
if(dic[i][j]==cheack[k])
{
j++;
k++;
cnt++;
}
else
{
j++;
}
if(j==len2||k==len1)
break;
}
if(cnt==len2-1)               //长度少一,但其是字典中某字符串的子串符合要求
printf(" %s",dic[i]);
}
else if(len1==len2+1)
{
cnt=0;
j=0;
k=0;
while(1)
{
if(dic[i][j]==cheack[k])
{
j++;
k++;
cnt++;
}
else
{
k++;
}
if(j==len2||k==len1)
break;
}
if(cnt==len1-1)               //长度多一即比字典中某字符串多一个字母符合要求
printf(" %s",dic[i]);
}
}
printf("\n");
}
}
return 0;
}<strong>
</strong>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: