首页 > 其他 > 详细

两道相似KMP题

时间:2014-02-22 23:07:06      阅读:414      评论:0      收藏:0      [点我收藏+]

1.POJ 3450 Coporate Identity

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

下面贴出其比较。

 

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

bubuko.com,布布扣
#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)

bubuko.com,布布扣
#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)

bubuko.com,布布扣
#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)

bubuko.com,布布扣
#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

两道相似KMP题

原文:http://www.cnblogs.com/whatbeg/p/3560775.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!