spell checking

                    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: 
(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 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



注:本例程序有些许问题,有待进一步改进:

#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;
}

原文地址:https://www.cnblogs.com/zdblog/p/3669124.html