zoukankan      html  css  js  c++  java
  • POJ1035 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;    
    }
  • 相关阅读:
    instanceof操作符判断对象类型
    继承
    题解 P3943 星空
    NOIP 模拟 10 考试总结
    题解 P3942 将军令
    题解 P3941 入阵曲
    题解 P3191 [HNOI2007]紧急疏散EVACUATE
    NOIP 模拟 9 考试总结
    NOIP 模拟 9 分组
    NOIP 模拟 9 数颜色
  • 原文地址:https://www.cnblogs.com/Lyush/p/2583893.html
Copyright © 2011-2022 走看看