两道相似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] = '\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] = '\0';
                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] == '\0')
            cout<<"IDENTITY LOST\n";
        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] = '\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] = '\0';
                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] == '\0')
            cout<<"IDENTITY LOST\n";
        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] = '\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] = '\0';
                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] == '\0' || strlen(ans) < 3)
            cout<<"no significant commonalities\n";
        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] = '\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] = '\0';
                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] == '\0' || strlen(ans) < 3)
            cout<<"no significant commonalities\n";
        else
            cout<<ans<<endl;
    }
    return 0;
}
View Code

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值