在openjudge上遇到了一个问题,却一直是wa,不知道哪里错了,希望懂c语言的人帮忙指出,万谢!(我的代码附在最下面)
拼写检查
查看提交统计提问
总时间限制: 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
我的代码:
#include
#include
int main()
{
char s[20][20];
int i=0,n;
for(i;;i++)
{
scanf("%s",s[i]);
if(s[i][0]=='#')
break;
}
n=i,i=0;
char a[20];
while(scanf("%s",a)&&a[0]!='#')
{
int b=0;
for(int j=0;j<=n-1;j++)
if(strcmp(a,s[j])==0)
{
b=1;
printf("%s is correct\n",a);
}
if(b==0)
{
printf("%s:",a);
for(int j=0;j<=n-1;j++)
{
if(strlen(s[j])==strlen(a))
{
int c=0;
int L=strlen(a);
for(int k=0;k<=L-1;k++)
if(s[j][k]!=a[k])
c++;
if(c==1)
printf(" %s",s[j]);
}
if(strlen(s[j])==strlen(a)+1)
{
int c=0;
int m=0;
for(int k=0;kif(a[m]!=s[j][k])
c++;
else
m++;
}
if(c==1)
printf(" %s",s[j]);
}
if(strlen(s[j])+1==strlen(a))
{
int c=0;
int m=0;
for(int k=0;kif(a[k]!=s[j][m])
c++;
else
m++;
}
if(c==1)
printf(" %s",s[j]);
}
}
printf("\n");
}
}
return 0;
}
“Devil组”引证GPT后的撰写:
在检查单词相似性的时候,需要注意以下几点:
对于第一种情况,即删除一个字符后是否相同,你的代码已经正确处理。
对于第二种情况,即替换一个字符后是否相同,你需要对字符串中的每个字符进行遍历,分别替换成其他字符,然后与词典中的单词进行比较,以确定是否相同。
对于第三种情况,即添加一个字符后是否相同,你需要对字符串中的每个位置进行遍历,分别添加一个字符,然后与词典中的单词进行比较,以确定是否相同。
修改后的代码:
#include<stdio.h>
#include<string.h>
int main()
{
char dict[10000][20]; // 词典中最多10000个单词,每个单词最长为15
int n = 0; // 词典中单词的数量
char word[20]; // 需要查询的单词
// 读入词典
while(scanf("%s", dict[n]) && dict[n][0] != '#') {
n++;
}
// 读入需要查询的单词
while(scanf("%s", word) && word[0] != '#') {
int found = 0; // 是否在词典中找到了单词
printf("%s:", word);
// 在词典中查找与需要查询的单词相同的单词
for(int i = 0; i < n; i++) {
if(strcmp(word, dict[i]) == 0) {
found = 1;
printf(" %s", word);
break;
}
}
// 在词典中查找与需要查询的单词相似的单词
for(int i = 0; i < strlen(word); i++) {
// 情况1:删除一个字母
char temp[20];
int index = 0;
for(int j = 0; j < strlen(word); j++) {
if(j != i) {
temp[index] = word[j];
index++;
}
}
temp[index] = '\0';
for(int j = 0; j < n; j++) {
if(strcmp(temp, dict[j]) == 0) {
found = 1;
printf(" %s", dict[j]);
}
}
// 情况2:替换一个字母
for(int k = 0; k < 26; k++) {
temp[i] = 'a' + k;
if(strcmp(temp, word) != 0) {
for(int j = 0; j < n; j++) {
if(strcmp(temp, dict[j]) == 0) {
found = 1;
printf(" %s", dict[j]);
}
}
}
}
// 情况3:增加一个字母
temp[index] = word[i];
temp[index+1] = '\0';
for(int k = 0; k < 26; k++) {
temp[i] = 'a' + k;
for(int j = 0; j < n; j++) {
if(strcmp(temp, dict[j]) == 0) {
found = 1;
printf(" %s", dict[j]);
}
}
}
}
if(found == 0) {
printf(" ");
}
printf("\n");
}
return 0;
}