您的位置:首页 > 其它

Spell checker 字典树&&普通查找(加计数)

2013-10-30 19:46 162 查看
[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#

[align=left]Sample Output[/align]

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<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
char str1[10101][55],str2[55];
int i,j;
class node
{
public:
node *p[26];
int it;
bool de;
node()
{
for(it=0;it<26;it++)
p[it]=NULL;
de=false;;
}
};
node *root;
void get_num(char*s)
{
node*r=root;
int len1=strlen(s);
int it;
for(it=0;it<len1;it++)
{
if(r->p[s[it]-'a']==NULL)
{
r->p[s[it]-'a']=new node();
}
r=r->p[s[it]-'a'];
}
r->de=true;
}
bool find(char *s)
{
int len2=strlen(s);
int it;
node *r=root;
for(it=0;it<len2;it++)
{
if(r->p[s[it]-'a']==NULL)
return false;
r=r->p[s[it]-'a'];
}
if(r->de)
return true;
return false;
}
bool solve(char s1[],char s2[])
{
int len1=strlen(s1);
int len2=strlen(s2);
int ncount=0;
if(len1!=len2)
{
int it=0,jt=0;
while(it<len1&&jt<len2)
{
if(s1[it]!=s2[jt])
{
ncount++;
if(len1>len2)
{
it++;
}
else
jt++;
}
else
{
it++;
jt++;
}
}
if(ncount>1)
return false;
else
return true;
}
else
{
int it=0;
while(it<len1)
{
if(s1[it]!=s2[it])
ncount++;
it++;
}
if(ncount==1)
return true;
else
return false;
}
}
int main()
{
int st=0;
root=new node();
while(scanf("%s",str1[st])&&str1[st][0]!='#')
{
get_num(str1[st]);
st++;
}
while(scanf("%s",str2)&&str2[0]!='#')
{
if(find(str2))//判断是否有
printf("%s is correct\n",str2);
else
{
printf("%s:",str2);
for(i=0;i<st;i++)
{
int temp=strlen(str1[i])-strlen(str2);
if(temp<=1&&temp>=-1)
if(solve(str1[i],str2))//判断是否有一个不同或可替换
printf(" %s",str1[i]);
}
printf("\n");
}
}
return 0;

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: