POJ 1035 字串

來源:互聯網
上載者:User

題目大意:給定一個字典,然後給定一組單詞集合,如果該單詞在字典中,則輸出correct;否則,如果該單詞能夠通過1>插入一個字母 2>刪除一個字母 3>替換一個字母三種操作中的一種轉化為字典序中的某個單詞,輸出該單詞。直接暴力過的,c++能過,g++逾時。

//11222230c00h00g1035Accepted1200K500MSC++1530B2013-01-30 15:07:29#include<string>#include<iostream>#include<map>using namespace std;string word[10005];map<string,int> mp;//判斷單詞能否替換 bool check_replace(string a,string b,int len){    int res=0;    for(int i=0;i<len;i++)        if(a[i]!=b[i]) res++;    if(res==1) return true;    return false;}//判斷能否通過插入或者刪除,如果插入,將長度小的字串寫在前面 bool check_insert(string a,string b,int len1,int len2){    int i=0,j=0;    while(i<len1&&j<len2&&a[i]==b[j]){i++;j++;}    j++;//b向後移動一個位置     while(i<len1&&j<len2&&a[i]==b[j]){i++;j++;}    if(i==len1&&j==len2) return true;    return false; }int main(){    int i=0;    while(cin>>word[i]&&word[i]!="#"){        mp[word[i]]=1;        i++;    }    int n=i;    string test;    while(cin>>test&&test!="#"){        if(mp[test])            cout<<test<<" is correct"<<endl;        else{           cout<<test<<":";           int len1=test.length();           for(int i=0;i<n;i++){               int len2=word[i].length();               if(len1==len2){                   if(check_replace(test,word[i],len1))                       cout<<" "<<word[i];               }else if(len1+1==len2){//insert a char                   if(check_insert(test,word[i],len1,len2))                       cout<<" "<<word[i];               }else if(len1-1==len2){//delete a char                   if(check_insert(word[i],test,len2,len1))                       cout<<" "<<word[i];               }           }           cout<<endl;        }            }    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.