zoukankan      html  css  js  c++  java
  • HDU

    input:

    10
    this
    is
    a
    dictionary
    that
    we
    will
    use
    for
    us
    6
    su
    as
    the
    dictonary
    us
    willl
    

    output:

    su is a misspelling of us
    as is a misspelling of is
    the is unknown
    dictonary is a misspelling of dictionary
    us is correct
    willl is a misspelling of will
    

     题目大意:

    单词有以下三种错误:1.多拼或少拼一个字符 2.拼错了一个字符 3.两个相邻的字符反了 4.不符合上述3种的为未知单词。
    

     分析:

    暴力模拟。根据4种错误情况和正确的情况直接经行模拟。
    1.能找到相同单词
    2.同长度的错误:(1)一个字符不同 (2)两个相邻字符反了。
    (利用一个一个wrongCnt来记录有几个不同字符,1个则为(1)错误,2个则为(2)错误,其他,则需要继续判断)
    3.长度差一的错误: (1)多了一个字符 (2)少了一个字符。
    (利用一个sum来记录有多少个不同,1个为(1)或者为(2)的错误,其他则需要继续判断)
    4.不满足2、3且找不到相等单词的错误(unknown)
    

     code:

    #define frp
    
    #include<bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    const ll INF = 0x3f3f3f3f;
    const ll inf = 0x7fffff;
    const int maxn = 1e8;
    const int MAXN = 10000;
    int n, q;
    string simpleArray[MAXN + 10];
    
    
    
    void solve() {
        cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> simpleArray[i];
        }
        cin >> q;
        for (int i = 0; i < q; i++) {
            bool right = false;
            string testString;
            cin >> testString;
    //        cout << testString << ":
    ";
            for (int j = 0; j < n; j++) {
                if (simpleArray[j] == testString) {
                    right = true;
                    cout << testString << " is correct" << endl;
                    break;
                }
            }
            if (!right) {
                bool isknown = true;
                for (int j = 0; j < n; j++) {
                    int lenTest = testString.size(), lenSimple = simpleArray[j].size();
                    int wrongCnt = 0;
                    int pointOne, pointTwo;
                    pointOne = -1;
                    pointTwo = -1;
                    //一个字符错误或者两个字符反了
                    if (lenTest == lenSimple) {
                        for (int k = 0; k < lenTest; k++) {
                            if (testString[k] != simpleArray[j][k]) {
                                wrongCnt++;
                                if (wrongCnt == 1) {
                                    pointOne = k;
                                } else if (wrongCnt == 2) {
                                    pointTwo = k;
                                } else {
                                    break;
                                }
                            }
                        }
                        if (wrongCnt == 1) {
                            cout << testString << " is a misspelling of " << simpleArray[j] << endl;
                            isknown = false;
                            break;
                        } else if (wrongCnt==2) {
                            if (testString[pointOne] == simpleArray[j][pointTwo] &&
                                testString[pointTwo] == simpleArray[j][pointOne]) {
                                cout << testString << " is a misspelling of " << simpleArray[j] << endl;
                                isknown = false;
                                break;
                            }
                        }
                    }
                    //多一个字符或者少一个字符
                    if(abs(lenSimple-lenTest)==1){
                        int xx=0,sum=0;
                        if(lenTest+1==lenSimple){
                            for(int k=0;k<lenSimple;k++){
                                if(testString[xx]==simpleArray[j][k]){
                                    xx++;
                                }else{
                                    sum++;
                                }
                            }
                        }else if(lenSimple+1==lenTest){
                            for(int k=0;k<lenTest;k++){
                                if(testString[k]==simpleArray[j][xx]){
                                    xx++;
                                }else{
                                    sum++;
                                }
                            }
                        }
                        if(sum==1){
                            cout<<testString<<" is a misspelling of "<<simpleArray[j]<<endl;
                            isknown=false;
                            break;
                        }
                    }
                }
                //不存在的单词
                if (isknown) {
                    cout << testString << " is unknown" << endl;
                }
            }
        }
    }
    
    int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    #ifdef frp
        freopen("D:\coding\c_coding\in.txt", "r", stdin);
    //    freopen("D:\coding\c_coding\out.txt", "w", stdout);
    #endif
        solve();
        return 0;
    }
    

      

  • 相关阅读:
    python 练习洗牌
    python 生成二维码
    转载 HTTP协议
    分别使用python和java练习冒泡排序
    python-字符串
    [转]三层架构与MVC之间的区别
    [转]JAVA异常
    [转]JavaEE开发基础
    [转]JAVA对象容器
    数据库操作实例
  • 原文地址:https://www.cnblogs.com/visualVK/p/9675107.html
Copyright © 2011-2022 走看看