Spell checker
Problem Description
You, asa member of a development team for a new spell checking program, are to write amodule that will check the correctness of given words using a known dictionaryof all correct words in all their forms.
If theword is absent in the dictionary then it can be replaced by correct words (fromthe dictionary) that can be obtained by one of the following operations:
?deletingof one letter from the word;
?replacingof one letter in the word with an arbitrary letter;
?insertingof one arbitrary letter into the word.
Yourtask is to write the program that will find all possible replacements from thedictionary for every given word.
Input
Thefirst part of the input file contains all words from the dictionary. Each wordoccupies its own line. This part is finished by the single character '#' on aseparate line. All words are different. There will be at most 10000 words inthe dictionary.
Thenext 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 be checked.
Allwords 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
Writeto the output file exactly one line for every checked word in the order oftheir 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 theirappearance in the dictionary (in the first part of the input file). If thereare no replacements for this word then the line feed should immediately followthe 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 myme
contestis correct
hav:has have
oo: too
or:
i iscorrect
fi: i
mre:more me
#include<bits/stdc++.h>
using namespace std;
set<string> wl;
struct node
{
char s[20];
int mod;
};
int thesame(char *a,char *b)
{
int alen,blen,num,i;
alen=strlen(a);
blen=strlen(b);
if(abs(alen-blen)>1)
{
return 0;
}
num=0;
if(alen>blen)
{
for(i=0;i<alen;i++)
{
if(a[i]==b[num])
{
num++;
}
}
if(num==blen)
{
return 1;
}
return 0;
}
else if(blen>alen)
{
for(i=0;i<blen;i++)
{
if(b[i]==a[num])
{
num++;
}
}
if(num==alen)
{
return 1;
}
return 0;
}
else
{
for(i=0;i<alen;i++)
{
if(a[i]==b[i])
{
num++;
}
}
if(num==alen-1)
{
return 1;
}
else
{
return 0;
}
}
}
int main()
{
wl.clear(); //字母表数组清零
node stu[10008]; //临时表作用及输入表作用
char str[20];
int i,j;
j=0;
while(~scanf("%s",&str))
{
if(str[0]=='#') break;
strcpy(stu[j].s,str);//写入临时表中
wl.insert(stu[j].s); //存入字典中
j++;
}
while(~scanf("%s",&str))
{
if(str[0]=='#') break;
if(wl.count(str)) //特别的函数,count能搜索wl中和str一样的内容,返回1或0
{
printf("%s is correct\n",str);
}
else
{
printf("%s:",str);
for(i=0;i<j;i++)
{
if(thesame(stu[i].s,str))
{
printf(" %s",stu[i].s);
}
}
printf("\n");
}
}
}