poj 1035 Spell checker

Spell checke
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24502 Accepted: 8947

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: 
?deleting of one letter from the word; 
?replacing of one letter in the word with an arbitrary letter; 
?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

Source



/*
题目大意:前面输入一系列字符串,以“#”结束,后面在输入一系列字符串,同样以“#”结束,问后面的字符串可以由前面哪些字符串通过添加,删除,替换一个字母得来,按照字典序输出,如果后面的和前面的相同,则直接输出xx is correct。
解题方法:先将前面的字符串保存到一颗字典树当中,以此来判断后面的字符串在前面是否存在,如果不存在再另作判断,看后面的字符串可以由前面哪些字符串通过添加,删除,替换一个字母得来。
*/
我发现这题可以直接暴力哎!哈哈!一次过

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
using namespace std;
int n,m;
char ch[10005][50];
char ch1[50];
struct node
{
    char cun[10005][50];
    int s;
}a;
int len[10005];
void scan()
{
    int i = 0;
    int j;
    int k;
    while(~scanf("%s",ch[i]))
    {
        if(strcmp(ch[i],"#") == 0)
        {
            break;
        }
        len[i] = strlen(ch[i]);
        i++;
    }
    n = i;
    i = 0;
    while(~scanf("%s",ch1))
    {
        a.s = 0;                         //要更新为零
        int ok = 0;
        if(strcmp(ch1,"#") == 0)
        {
            break;
        }
        int len1 = strlen(ch1);
        for(j = 0;j < n; j++)
        {
            ok = 10;                                        //这里的ok指的是你有几个不同,但这里为什么是10呢?一会告诉你
            if(len1 == len[j])                           //当字符一样长时
            {
                ok = 0;                                       //ok要更新为0
                for(k = 0;k < len1; k++)              //一点一点的比较
                {
                    if(ch[j][k] != ch1[k])              //发现不匹配时
                    {
                        ok++;
                    }
                    if(ok == 2)                        //当是2时直接跳出
                    {
                        break;
                    }
                }
                if(ok == 0)                            //当完全匹配时,跳出,因为一样的字符  输出不一样
                {
                    break;
                }
                if(ok == 1)                            //当只是一时要存字符串
                {
                    strcpy(a.cun[a.s],ch[j]);
                    a.s++;                                 //当前存入字符串的个数,所以在上面要更新为0
                }
            }
            else if(len1 == len[j]+1)               //当差一时
            {
                ok = 0;
                int e = 0;
                for(k = 0;k < len[j]; k++)
                {
                    if(ch[j][k] != ch1[e])
                    {
                        k--;                                     //小的那个要--
                        ok++;
                    }
                    e++;
                    if(ok == 2)
                    {
                        break;
                    }
                }
                if(ok == 1||ok == 0)                 //这里为什么是0,1都要存呢?,因为字符长度差一时不可能一样了,,,是吧,ok可能为0,因为可能就最后一个多出来
                {
                    strcpy(a.cun[a.s],ch[j]);
                    a.s++;
                }
                ok = 10;                    //千万不要忘了更新ok,为什么?后面告诉你
            }
            else if(len1+1 == len[j])    //同理
            {
                ok = 0;
                int e = 0;
                for(k = 0;k < len1; k++)
                {
                    if(ch[j][e] != ch1[k])
                    {
                        k--;
                        ok++;
                    }
                    e++;
                    if(ok == 2)
                    {
                        break;
                    }
                }
                if(ok == 1||ok == 0)
                {
                    strcpy(a.cun[a.s],ch[j]);
                    a.s++;
                }
                ok = 1;
            }
        }
        if(ok == 0)               //ok == 0就表示字符一样,当你一开始就设为0,那完全不相等的字符串怎么办?,,所以以上三次都要把ok设为不为零的数,我随便设为10了
        {
            printf("%s is correct\n",ch1);
        }
        else
        {
            printf("%s:",ch1);
            for(i = 0;i < a.s; i++)
            {
                printf(" %s",a.cun[i]);
            }
            printf("\n");
        }
    }
}
int main()
{
    scan();//原来我还想用函数的,但是没用上,就这样孤零零的好可怜,,
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值