您的位置:首页 > 其它

POJ 1035 Spell checker

2018-01-26 12:27 453 查看
题意:有一个字典,里面有小于10000个不同的单词,先在有一序列的单词需要进行检查,序列长度(检查单词个数)不大于50。

对于每一个要检查的单词,如果在字典中找到一模一样的,输出,ckword is correct;

如果检查的单词,改变一个字母,与字典中的单词一模一样,输出字典中的单词;

如果检查的单词,增加一个字母,与字典中的单词一模一样,输出字典中的单词;

如果检查的单词,减少一个字母,与字典中的单词一模一样,输出字典中的单词;即可。

思想:对字符串的处理,如果串一样,那么属于第一种情况,输出ckword iscorrect

如果字典中的单词和检查的单词的字母长度相差大于等于2显然字典中的该单词必定不是要输出的单词;

如果字典中的单词和检查的单词的字母长度相差等于1,那么可以增加,或减少一个单词,再比较两串异同(此处不同做标记即可);

如果字典中的单词和检查的单词的字母长度相差等于0,如果两个单词只有一个字母不一样,就可以将字典中的单词输出。

#include<iostream>
#include<cstring>
using namespace std;
struct node
{
char word[16];
}dic[10005];
int main()
{
int cnt=0;
while(true)
{
cin>>dic[++cnt].word;
if(strcmp(dic[cnt].word,"#")==0) break;
}
cnt--;
while(true)
{
int tmp=0;
int ckLen;
char ckword[17];
cin>>ckword;
if(strcmp(ckword,"#")==0) break;
for(int i=1;i<=cnt;i++)
{
if(strcmp(ckword,dic[i].word)==0)
{
tmp=1;
break;
}
}
if(tmp==1)
{
cout<<ckword<<" is correct"<<endl;
continue;
}
ckLen=strlen(ckword);
cout<<ckword<<":";
for(int i=1;i<=cnt;i++)
{
int dicLen=strlen(dic[i].word);
tmp=ckLen-dicLen;
int mark;
switch(tmp)
{
case 0://只有一个不一样
mark=0;
for(int j=0;j<ckLen;j++)
{
if(ckword[j]!=dic[i].word[j])
mark++;
if(mark>1) break;
}
if(mark<=1) cout<<" "<<dic[i].word;
break;
case 1://测试串长,去掉一个
mark=0;
for(int j=0,k=0;j<ckLen,k<dicLen;)
{
if(ckword[j]!=dic[i].word[k])
{
j++;
mark++;
}
else
{
j++;k++;
}
if(mark>1) break;
}
if(mark<=1) cout<<" "<<dic[i].word;
break;
case -1://测试串短,添加一个
mark=0;
for(int j=0,k=0;j<ckLen,k<dicLen;)
{
if(ckword[j]!=dic[i].word[k])
{
k++;
mark++;
}
else
{
j++;k++;
}
if(mark>1) break;
}
if(mark<=1) cout<<" "<<dic[i].word;
default:
break;
}
}
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: