POJ 1035 Spell checker

按照 CSGrandeur 大牛的给出的方法做的;

暴力即可;

判断是否可以添加一个 字符得到时,有点小技巧,具体见代码;

# include <stdio.h>

# include <string.h>



# define WL 17



int n;

char dic[10005][WL], word[WL];



char exist(char *buf)

{

    int i;



    for (i = 0; i < n; ++i)

    {

        if (strcmp(dic[i], word) == 0) return 1;

    }



    return 0;

}



char replace(char *x, char *y)

{

    int i, lenx, leny, cnt;



    lenx = strlen(x), leny = strlen(y);

    if (lenx != leny) return 0;

    for (cnt = 0, i = 0; i < lenx; ++i)

    {

        if (x[i] != y[i]) ++cnt;

        if (cnt > 1) return 0;

    }

    return 1;

}



char add(char *x, char *y)

{

    int i, j, lenx, leny, cnt;



    lenx = strlen(x), leny = strlen(y);

    if (leny != lenx+1) return 0;

    for (cnt = 0, i = 0, j = 0; i < leny; ++i, ++j)

    {

        if (x[i] != y[j]) ++cnt, --i;

        if (cnt > 1) return 0;

    }

    return 1;

}



void find(char *buf)

{

    int i;



    printf(":");

    for (i = 0; i < n; ++i)

    {

        if (replace(dic[i], buf) || add(buf, dic[i]) || add(dic[i], buf))

            printf(" %s", dic[i]);

    }

}



int main()

{

    for (n = 0; scanf("%s", dic[n]), dic[n][0] != '#'; ++n) ;

    for ( ; scanf("%s", word), word[0] != '#'; )

    {

        printf("%s", word);

        if (exist(word)) printf(" is correct");

        else find(word);

        putchar('\n');

    }



    return 0;

}

//

你可能感兴趣的:(check)