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

来源:互联网 发布:一知f君百度网盘郑子豪 编辑:程序博客网 时间:2024/05/04 00:55

题目链接: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 LOCALset<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 LOCALfreopen("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
原创粉丝点击