您的位置:首页 > 其它

POJ 1035 Spell checker (串)

2014-07-21 16:04 351 查看
题目大意:

问你后面输入的串能不能通过 加减一个字符,或者替换一个字符变成字典中的串。

思路分析:

直接模拟替换加减的过程。

比较两个串的长度。要相差为1 的时候才能进行模拟。

模拟的过程就是进行一个个的匹配。

发现失配的次数小于等于 1就可以输出。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#define maxn 10005
using 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: