• 两道相似KMP题


    1.POJ 3450 Coporate Identity

    这两题的解法都是枚举子串,然后匹配,像这种题目以后可以不用KMP来做,直接字符串自带的strstr函数搞定,如果字符串未出现,该函数返回NULL。

    下面贴出其比较。

    代码:(KMP版)(1360ms 888KB)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define N 4007
    
    char ans[203],str[203];
    char ss[N][203],tt[203];
    int next[203];
    
    void getnext(char *ss)
    {
        int m = strlen(ss);
        next[0] = -1;
        int i = 0,j = -1;
        while(i<m)
        {
            if(j == -1 || ss[i] == ss[j])
                next[++i] = ++j;
            else
                j = next[j];
        }
    }
    
    int kmp(char *ss,char *tt)
    {
        int n = strlen(ss);
        int m = strlen(tt);
        getnext(tt);
        int i = -1,j = -1;
        while(i<n && j<m)
        {
            if(j == -1 || ss[i] == tt[j])
                i++,j++;
            else
                j = next[j];
        }
        if(j == m)
            return 1;
        return 0;
    }
    
    int main()
    {
        int n,i,len,j,k,lengh;
        while(scanf("%d",&n)!=EOF && n)
        {
            for(i=0;i<n;i++)
                scanf("%s",ss[i]);
            lengh = strlen(ss[0]);
            ans[0] = '';
            for(len=1;len<=lengh;len++)  //枚举长度
            {
                for(i=0;i<=lengh-len;i++)  //枚举起点
                {
                    for(k=0,j=i;j<i+len;j++) //取出此字串
                        str[k++] = ss[0][j];
                    str[k] = '';
                    for(j=1;j<n;j++)
                    {
                        if(!kmp(ss[j],str))
                            break;
                    }
                    if(j == n)
                    {
                        if(strlen(ans) == len && strcmp(ans,str) > 0)
                            strcpy(ans,str);
                        if(strlen(ans) < len)
                            strcpy(ans,str);
                    }
                }
            }
            if(ans[0] == '')
                cout<<"IDENTITY LOST
    ";
            else
                cout<<ans<<endl;
        }
        return 0;
    }
    View Code

    代码:(strstr函数版)(454ms 912KB)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <cstdlib>
    using namespace std;
    #define N 4007
    
    char ans[203],str[203];
    char ss[N][203],tt[203];
    int main()
    {
        int n,i,len,j,k,lengh;
        while(scanf("%d",&n)!=EOF && n)
        {
            for(i=0;i<n;i++)
                scanf("%s",ss[i]);
            lengh = strlen(ss[0]);
            ans[0] = '';
            for(len=1;len<=lengh;len++)  //枚举长度
            {
                for(i=0;i<=lengh-len;i++)  //枚举起点
                {
                    for(k=0,j=i;j<i+len;j++) //取出此字串
                        str[k++] = ss[0][j];
                    str[k] = '';
                    for(j=1;j<n;j++)
                    {
                        if(strstr(ss[j],str) == NULL)
                            break;
                    }
                    if(j == n)
                    {
                        if(strlen(ans) == len && strcmp(ans,str) > 0)
                            strcpy(ans,str);
                        if(strlen(ans) < len)
                            strcpy(ans,str);
                    }
                }
            }
            if(ans[0] == '')
                cout<<"IDENTITY LOST
    ";
            else
                cout<<ans<<endl;
        }
        return 0;
    }
    View Code

    2.POJ 3080 Blue Jeans

    代码:(KMP版)(32ms 684KB)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define N 4007
    
    char ans[65],str[65];
    char ss[12][65],tt[65];
    int next[65];
    
    void getnext(char *ss)
    {
        int m = strlen(ss);
        next[0] = -1;
        int i = 0,j = -1;
        while(i<m)
        {
            if(j == -1 || ss[i] == ss[j])
                next[++i] = ++j;
            else
                j = next[j];
        }
    }
    
    int kmp(char *ss,char *tt)
    {
        int n = strlen(ss);
        int m = strlen(tt);
        getnext(tt);
        int i = -1,j = -1;
        while(i<n && j<m)
        {
            if(j == -1 || ss[i] == tt[j])
                i++,j++;
            else
                j = next[j];
        }
        if(j == m)
            return 1;
        return 0;
    }
    
    int main()
    {
        int n,i,len,j,k,lengh,t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(i=0;i<n;i++)
                scanf("%s",ss[i]);
            lengh = strlen(ss[0]);
            ans[0] = '';
            for(len=1;len<=lengh;len++)  //枚举长度
            {
                for(i=0;i<=lengh-len;i++)  //枚举起点
                {
                    for(k=0,j=i;j<i+len;j++) //取出此字串
                        str[k++] = ss[0][j];
                    str[k] = '';
                    for(j=1;j<n;j++)
                    {
                        if(!kmp(ss[j],str))
                            break;
                    }
                    if(j == n)
                    {
                        if(strlen(ans) == len && strcmp(ans,str) > 0)
                            strcpy(ans,str);
                        if(strlen(ans) < len)
                            strcpy(ans,str);
                    }
                }
            }
            if(ans[0] == '' || strlen(ans) < 3)
                cout<<"no significant commonalities
    ";
            else
                cout<<ans<<endl;
        }
        return 0;
    }
    View Code

    代码:(strstr函数版)(0ms 700KB)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define N 4007
    
    char ans[65],str[65];
    char ss[12][65];
    
    int main()
    {
        int n,i,len,j,k,lengh,t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(i=0;i<n;i++)
                scanf("%s",ss[i]);
            lengh = strlen(ss[0]);
            ans[0] = '';
            for(len=1;len<=lengh;len++)  //枚举长度
            {
                for(i=0;i<=lengh-len;i++)  //枚举起点
                {
                    for(k=0,j=i;j<i+len;j++) //取出此字串
                        str[k++] = ss[0][j];
                    str[k] = '';
                    for(j=1;j<n;j++)
                    {
                        if(strstr(ss[j],str) == NULL)
                            break;
                    }
                    if(j == n)
                    {
                        if(strlen(ans) == len && strcmp(ans,str) > 0)
                            strcpy(ans,str);
                        if(strlen(ans) < len)
                            strcpy(ans,str);
                    }
                }
            }
            if(ans[0] == '' || strlen(ans) < 3)
                cout<<"no significant commonalities
    ";
            else
                cout<<ans<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    JS学习笔记-OO疑问之对象创建
    文件系统类型:
    Swift 编程语言新手教程
    数组长度计算
    tomcat配置文件server.xml具体解释
    openGL点精灵PointSprite具体解释: 纹理映射,旋转,缩放,移动
    iOS安全攻防(三):使用Reveal分析他人app
    逍遥叹
    oracle存储过程实例
    Java爬虫
  • 原文地址:https://www.cnblogs.com/whatbeg/p/3560775.html
Copyright © 2020-2023  润新知