拼字检索——

该程序设计了一个模块,用于检查给定单词是否在已知的正确单词字典中。如果不在,它会通过删除、替换或插入一个字母找到可能的正确替换词。输入包括字典和待检查的单词列表,输出是对每个单词的检查结果,列出正确的单词或其可能的替换选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spell checker

Problem Description

You, asa member of a development team for a new spell checking program, are to write amodule that will check the correctness of given words using a known dictionaryof all correct words in all their forms.

If theword is absent in the dictionary then it can be replaced by correct words (fromthe dictionary) that can be obtained by one of the following operations:

?deletingof one letter from the word;

?replacingof one letter in the word with an arbitrary letter;

?insertingof one arbitrary letter into the word.

Yourtask is to write the program that will find all possible replacements from thedictionary for every given word.

Input

Thefirst part of the input file contains all words from the dictionary. Each wordoccupies its own line. This part is finished by the single character '#' on aseparate line. All words are different. There will be at most 10000 words inthe dictionary.

Thenext part of the file contains all words that are to be checked. Each wordoccupies 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.

Allwords in the input file (words from the dictionary and words to be checked)consist only of small alphabetic characters and each one contains 15 charactersat most.

Output

Writeto the output file exactly one line for every checked word in the order oftheir 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 theirappearance in the dictionary (in the first part of the input file). If thereare no replacements for this word then the line feed should immediately followthe 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 myme

contestis correct

hav:has have

oo: too

or:

i iscorrect

fi: i

mre:more me

#include<bits/stdc++.h>
using namespace std;

set<string> wl; 
struct node
{
    char s[20];
    int mod;
};

int thesame(char *a,char *b) 
{
    int alen,blen,num,i;
    
    alen=strlen(a);
    blen=strlen(b);
    if(abs(alen-blen)>1)
    {
        return 0;
    }
    
    num=0;
    if(alen>blen)
    {
        for(i=0;i<alen;i++)
        {
            if(a[i]==b[num])
            {
                num++;
            }
        }
        if(num==blen)
        {
            return 1;
        }
        return 0;
    }
    else if(blen>alen)
    {
        for(i=0;i<blen;i++)
        {
            if(b[i]==a[num])
            {
                num++;
            }
        }
        if(num==alen)
        {
            return 1;
        }
        return 0;
    }
    else
    {
        for(i=0;i<alen;i++)
        {
            if(a[i]==b[i])
            {
                num++;
            }
        }
        if(num==alen-1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}

int main()
{
    wl.clear(); //字母表数组清零 
    node stu[10008];  //临时表作用及输入表作用 
    char str[20];
    int i,j;
     
    j=0;
    while(~scanf("%s",&str))
    {
        if(str[0]=='#') break;
        strcpy(stu[j].s,str);//写入临时表中 
        wl.insert(stu[j].s); //存入字典中 
        j++; 
    }
    while(~scanf("%s",&str))
    {
        if(str[0]=='#') break;
        if(wl.count(str))  //特别的函数,count能搜索wl中和str一样的内容,返回1或0 
        {
            printf("%s is correct\n",str);
        }
        else
        {
            printf("%s:",str);
            for(i=0;i<j;i++)
            {
                if(thesame(stu[i].s,str))
                {
                    printf(" %s",stu[i].s);
                }
            }
            printf("\n");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值