Spell checker
Description
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:
(1)deleting of one letter from the word;
(2)replacing of one letter in the word with an arbitrary letter;
(3)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 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
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<iostream>
#include<string.h>
using namespace std;
int DictNum=0; //字典中字符的个数
int WordNum=0; //单词计算器
char dict[10001][16]; //数据库中的单词
char word[51][16]; //要操作的单词
int K=0;
int main()
{
void Input(); //输入函数
bool Change(char *word,char *list); //改变字符函数
bool Add(char *word,char *list); //增加字符函数
bool Delete(char *word,char *list); //删除字符函数
int *DictLen=new int[DictNum]; //记录词库中各个单词的长度
Input();
// cout<<"DictNum=="<<DictNum<<endl;
// cout<<"WordNum=="<<WordNum<<endl;
for(int i1=0;i1<DictNum;i1++) DictLen[i1]=strlen(dict[i1]); //得到词库中各单词的长度
// for(int i1=0;i1<DictNum;i1++) cout<<DictLen[i1]<<' ';
// cout<<endl;
for(int i=0;i<WordNum;i++) //遍历要操作的每一个单词
{
//cout<<"K=="<<K++<<endl;
bool flag=false; //标记字典中是否有单词word[i];
//cout<<"flag=="<<flag<<endl;
int len=strlen(word[i]); //求得该单词的长度
//cout<<"len=="<<len<<endl;
int pa=0; //记录多个与当前操作单词相似
//cout<<"pa=="<<pa<<endl;
int *address=new int[WordNum]; //记录要操作的单词通过变化得到的单词在dict中的下标
for(int j=0;j<DictNum;j++)
{
if(len==DictLen[j])
{
if(!strcmp(word[i],dict[j])) //词库中存在此单词
{
cout<<"比较后的结果1(匹配):"<<dict[j]<<endl;
flag=true;
break;
}
if(Change(word[i],dict[j]))
{
cout<<"比较后的结果2(改变):"<<dict[j]<<endl;
address[pa++]=j;
cout<<"dict[address[pa-1]]=="<<dict[address[pa-1]]<<endl;
}
}
else if((len-DictLen[j])==1)
{
if(Delete(word[i],dict[j]))
{
cout<<"比较后的结果(删除):"<<dict[j]<<endl;
address[pa++]=j;
}
}
else if((DictLen[j]-len)==1)
{
if(Add(word[i],dict[j]))
{
cout<<"比较后的结果(增加):"<<dict[j]<<endl;
address[pa++]=j;
}
}
}
cout<<"pa=="<<pa<<endl;
for(int k=0;k<pa;k++)
{
cout<<address[k]<<' ';
}
cout<<endl;
if(flag) cout<<word[i]<<" is correct"<<endl;
else
{
cout<<word[i]<<": ";
for(int i2=0;i2<pa;i2++)
cout<<dict[ address[i2] ]<<' ';
cout<<endl;
}
delete address;
}
delete DictLen;
return 0;
}
void Input()
{
while(cin>>dict[DictNum]&&dict[DictNum++][0]!='#');
while(cin>>word[WordNum]&&word[WordNum++][0]!='#');
DictNum--;
WordNum--;
}
bool Change(char *word,char *list)
{
cout<<"1111"<<endl;
int dif=0;
while(*word)
{
if(*(word++)!=*(list++))
{
dif++;
if(dif>1) return false;
}
}
return true;
}
bool Add(char *word,char *list)
{
cout<<"2222"<<endl;
int dif=0;
while(*list)
{
if(*(list++)!=*(word++))
{
word--;
dif++;
if(dif>1) return false;
}
}
return true;
}
bool Delete(char *word,char *list)
{
cout<<"3333"<<endl;
int dif=0;
while(*word)
{
if(*(word++)!=*(list++))
{
list--;
dif++;
if(dif>1) return false;
}
}
return true;
}