poj1035 Spell Checker

来源:互联网 发布:2017编程语言 编辑:程序博客网 时间:2024/04/28 14:49

题目:

1:拼写检查
查看 提交 统计 提问
总时间限制: 2000ms 内存限制: 65536kB
描述
现在有一些英语单词需要做拼写检查,你的工具是一本词典。需要检查的单词,有的是词典中的单词,有的与词典中的单词相似,你的任务是发现这两种情况。单词A与单词B相似的情况有三种:
1、删除单词A的一个字母后得到单词B;
2、用任意一个字母替换单词A的一个字母后得到单词B;
3、在单词A的任意位置增加一个字母后得到单词B。
你的任务是发现词典中与给定单词相同或相似的单词。


输入
第一部分是词典中的单词,从第一行开始每行一个单词,以"#"结束。词典中的单词保证不重复,最多有10000个。
第二部分是需要查询的单词,每行一个,以"#"结束。最多有50个需要查询的单词。
词典中的单词和需要查询的单词均由小写字母组成,最多包含15个字符。
输出
按照输入的顺序,为每个需要检查的单词输出一行。如果需要检查的单词出现在词典中,输出“?x is correct",?x代表需要检查的单词。如果需要检查的单词没有出现在词典中,则输出"?x: ?x1 ?x2 ...?xn",其中?x代表需要检查的单词,?x1...?xn代表词典中与需要检查的单词相似的单词,这些单词中间以空格隔开。如果没有相似的单词,输出"?x:"即可。

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

样例输出
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


======================================================================

Brute force...


代码清单:

#include <iostream>#include <cstdio>#include <cstdlib>#include <string.h>using namespace std;#define MAXLEN 16#define MAXENTRY 10005char dict[MAXENTRY][MAXLEN];bool isValid(int ind, char inq[]){int len1=strlen(dict[ind]);int len2=strlen(inq);int same=0, i, j;if (len1==len2){for (i=0; i<len1; ++i){if (dict[ind][i]==inq[i]){++same;}}if(same==len1-1)return true;}else if (len1==len2-1)//待查单词比字典单词多一个字母{for (i=0, j=0; i<len1&&j<len2;){if (dict[ind][i]==inq[j]){++same;++i;++j;}else++j;}if (same==len1)return true;}else if (len1==len2+1)//待查单词比字典单词少一个字母{for (i=0, j=0; i<len1&&j<len2;){if (dict[ind][i]==inq[j]){++same;++i;++j;}else++i;}if (same==len2)return true;}return false;}int main(){freopen("D:\\in.txt", "r", stdin);freopen("D:\\out.txt", "w", stdout);char current[MAXLEN];int entryNum=0;bool directHit;while (1){scanf("%s", dict[entryNum]);if (dict[entryNum][0]=='#') break;++entryNum;}while (1){scanf("%s", current);if(current[0]=='#') break;directHit=false;//直接能在字典中找到for (int i=0; i<entryNum; ++i){if (strcmp(dict[i], current)==0){printf("%s is correct\n", current);directHit=true;break;}}//以下是需要操作的情况if (!directHit){printf("%s:", current);for (int i=0; i<entryNum; ++i){if (isValid(i, current)){printf(" %s", dict[i]);}}printf("\n");}}return 0;}

0 0
原创粉丝点击