POJ 1035 Spell checker (串)

來源:互聯網
上載者:User

標籤:os   io   for   re   c   ar   

題目大意:

問你後面輸入的串能不能通過  加減一個字元,或者替換一個字元變成字典中的串。


思路分析:

直接類比替換加減的過程。

比較兩個串的長度。要相差為1 的時候才能進行類比。

類比的過程就是進行一個個的匹配。

發現失配的次數小於等於 1就可以輸出。


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <string>#include <map>#define maxn 10005using namespace std;char str[maxn][20];string tmp;map <string,bool>mymap;int main(){    while(scanf("%s",str[0])!=EOF)    {        mymap.clear();        tmp=str[0];        mymap[tmp]=true;        int i=1;        while(scanf("%s",str[i]) && str[i][0]!='#')        {            tmp=str[i];            mymap[tmp]=true;            i++;        }        char txt[20];        while(scanf("%s",txt) && txt[0]!='#')        {            tmp=txt;            if(mymap[tmp])printf("%s is correct\n",txt);            else            {                printf("%s:",txt);                int l=strlen(txt);                for(int j=0;j<i;j++)                {                    int len=strlen(str[j]);                    if(l+1==len)                    {                        int cnt=0;                        int p=0,k=0;                        while(p<l && k<len)                        {                            if(txt[p]==str[j][k]) p++,k++;                            else {                                cnt++;                                k++;                            }                        }                        if(cnt<=1 && p==l)printf(" %s",str[j]);                    }                    if(l==1+len)                    {                        int cnt=0;                        int p=0,k=0;                        while(p<len && k<l)                        {                            if(str[j][p]==txt[k])p++,k++;                            else {                                cnt++;                                k++;                            }                        }                        if(cnt<=1 && p==len)printf(" %s",str[j]);                    }                    if(l==len)                    {                        int cnt=0;                        for(int k=0;k<len;k++)if(txt[k]!=str[j][k])cnt++;                        if(cnt==1)printf(" %s",str[j]);                    }                }                puts("");            }        }    }    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.