HDU Automatic Correction of Misspellings (复杂模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1483

复杂模拟,在开始做之前一定要清晰的在草纸上写出模拟的思路和各个模块!!

锻炼代码能力!

另外发现了一个问题:s.length()-len < 0 时直接得出的结果非负数  会很大

例如下面


我们看到结果很大,这样就说明是错误的,我们可以先把s1.length()的值给另一个变量,然后再做减法,这样结果就对了

还有记得用s.erase(dex, amount);的时候注意s.length是变化的.


AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long LL;
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MAX_N 10005
//#define LOCAL
set<string> st;
string dic[MAX_N];
int N;
int solve(string now)
{
    int ret = INF;
    int len = now.length();
    for(int i = 0; i < N; i++)
    {
        string tdic = dic[i];
        int len2 = tdic.length();           //注意 不可以用 dic[i].length()-now.length(); 此处注意.length()是长整形,不知道为何数据很大
        if(abs(len2-len) > 1)    //长度过长或过短
        {
            continue;
        }

        if(tdic.length() == len)  //长度相等的时候2,3的情况
        {
            vector<int> dex;
            int cnt = 0;
            for(int j = 0; j < len; j++)
            {
                if(tdic[j] != now[j])
                {
                    cnt++;
                    dex.push_back(j);
                }
                if(cnt > 2) break;
            }
            if(cnt == 1)        //第二种情况
            {
                return i;
            }
            else
                if(cnt == 2)    //第三种情况
            {
                int fir = dex[0], sec = dex[1];
                if(tdic[fir] == now[sec] && tdic[sec] == now[fir])
                {
                    return i;
                }
            }
            dex.clear();
        }
        else
        {
            string chang, duan;
            if(tdic.length() > len)
            {
                chang = tdic;
                duan = now;
            }
            else
            {
                chang = now;
                duan = tdic;
            }
            for(int j = 0; j < chang.length(); j++)
            {
                if(chang[j] != duan[j])
                {
                    chang.erase(j,1);
                    break;
                }
            }
            if(chang.length() != duan.length())
                continue;
            bool have = 0;
            for(int j = 0; j < duan.length(); j++)
            {
                if(chang[j] != duan[j])
                {
                    have = 1;
                    break;
                }
            }
            if(!have)
                return i;
        }
    }
    return ret;
}
int main()
{
	#ifdef LOCAL
		freopen("b:\\data.in.txt", "r", stdin);
	#endif
    scanf("%d", &N);
    for(int i = 0; i < N; i++)
    {
        cin >> dic[i];
        st.insert(dic[i]);
    }
    int q; scanf("%d", &q);
    string que_s;
    set<string>::iterator it;
//    for(it=st.begin(); it!=st.end(); it++)
//    {
//        cout << *it << "-- " << endl;
//    }

    for(int i = 0; i < q; i++)
    {
        cin >> que_s;
        if(st.find(que_s) != st.end())
        {
            cout << que_s << " is correct" << endl;
        }
        else
        {
            int ret = solve(que_s);
            if(ret != INF)
            {
                cout << que_s << " is a misspelling of " << dic[ret] << endl;
            }
            else
                cout << que_s << " is unknown" << endl;
        }

    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值