两道相似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
原文地址:https://www.cnblogs.com/whatbeg/p/3560775.html