POJ-1035 Spell checker 暴力

直接暴力。

代码如下:

#include <cstring>

#include <cstdio>

#include <cstdlib>

#include <map>

#include <string>

using namespace std;



char word[10005][55], c[55];



map<string, int>mp;



bool Del(int x)

{

    int length = strlen(c), sum = 0;

    if (length != strlen(word[x])+1) {

        return false;

    }

    for (int i = 0, j = 0; i < length; ++i, ++j) {

        if (word[x][i] == c[j]) {

            continue;

        }

        else {

            ++sum;

            --i;

            if (sum == 2) {

                break;

            }

        }

    }

    return sum == 1;

}



bool Rep(int x)

{

    int length = strlen(c), sum = 0;

    if (length != strlen(word[x])) {

        return false;

    }

    for (int i = 0; i < length; ++i) {

        if (c[i] != word[x][i]) {

            ++sum;

            if (sum == 2) {

                break;

            }

        }

    }

    return sum == 1;

}



bool Ins(int x)

{

    int length = strlen(c), sum = 0;

    if (length != strlen(word[x])-1) {

        return false;

    }

    for (int i = 0, j = 0; i <= length; ++i, ++j) {

        if (c[i] == word[x][j]) {

            continue;

        }

        else {

            ++sum; 

            --i;

            if (sum == 2) {

                break;

            }

        }

    }

    return sum == 1;

}



bool OK(int x)

{

    if (Del(x) || Rep(x) || Ins(x)) {

        return true;

    }

    else {

        return false;

    }

}



int main()

{ 

    int cnt;

    for (cnt = 0; ; ++cnt) {

        scanf("%s", word[cnt]);

        mp[word[cnt]] = 1;

        if (word[cnt][0] == '#') {

            break;

        }

    }

    while (scanf("%s", c), c[0] != '#') {

        if (mp.count(c)) {

            printf("%s is correct\n", c);

            continue;

        }

        printf("%s:", c);

        for (int i = 0; i < cnt; ++i) {

            if (OK(i)) {

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

            }

        }

        puts("");

    }

    return 0;    

}

你可能感兴趣的:(check)