poj 1035 Spell checker

來源:互聯網
上載者:User

標籤:style   os   io   for   re   c   

題意:對於給出的一些單片語成字典,然後對於每一個輸入的單詞檢查,有以下幾種情況:

1.這個單詞在字典裡有

2.這個單詞刪掉任意一個字母能在字典裡有

3.這個單詞插入任意一個字母能在字典裡有

4.這個單詞任意一個字母被替換在字典裡有

這裡可以用string,細心一點,10000個字典單詞,50個查詢單詞,每個單詞長度不超過15,那麼可以對於每個查詢單詞暴力遍曆判斷一次

 

#include <iostream>#include <string>using namespace std;string dic[10005];//字典string ans[10005];//可能有的答案int sum,ans_sum;//字典單詞數,可能答案數string str;//臨時輸入的字串void Imput(){    sum = 0;    while(cin >> str)    {        if(str == "#") break;        dic[sum ++] = str;    }}void Print(){    cout << str << ':';    for(int i = 0; i < ans_sum; i ++) cout << ' ' << ans[i];    cout<<endl;}void check(){    while(cin >> str)    {        if(str == "#") break;        ans_sum = 0;        bool should_print = true;//是否要輸出,這裡對於 is correct 之後就不用後面的Print函數啦        for(int i = 0; i < sum; i ++)        {            if(str == dic[i]){                cout << str << " is correct" << endl;                should_print = false;                break;            }            else if(dic[i].length() == str.length()){ //長度相等,則可能有一個字母被替換                int flag = 0;                for(int j = 0; j < dic[i].length(); j ++)                {                    if(dic[i][j] != str[j]) flag ++;                }                if(flag == 1)                    ans[ans_sum ++] = dic[i];            }            else if(dic[i].length() - str.length() == 1){ //在str中插入一個字母,比較                int flag = 0;                for(int str_l = 0, dic_l = 0; str_l < str.length() && dic_l < dic[i].length(); )                {                    if(str[str_l] == dic[i][dic_l]) str_l ++, dic_l ++;                    else                    {                        dic_l ++;                        flag ++;                    }                }                if(!flag || flag == 1)                   ans[ans_sum++] = dic[i];            }            else if(str.length() - dic[i].length() == 1){ //刪除一個字母                int flag = 0;                for(int str_l = 0, dic_l = 0; str_l < str.length() && dic_l < dic[i].length(); )                {                    if(str[str_l] == dic[i][dic_l]) str_l ++, dic_l ++;                    else                    {                        str_l ++;                        flag ++;                    }                }                if(!flag || flag == 1)                   ans[ans_sum++] = dic[i];            }        }        if(should_print)  Print();    }}int main(){    Imput();    check();    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.