HDU1403-Longest Common Substring

本文介绍了一种求解两字符串最长公共子串的问题,并提供了一个详细的算法实现案例。通过构造特殊字符串并利用后缀数组和LCP数组,该算法能在较短时间内找到最长公共子串的长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Longest Common Substring

                                                                           Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                                                      Total Submission(s): 6177    Accepted Submission(s): 2185

Problem Description
Given two strings, you have to tell the length of the Longest Common Substring of them.

For example:
str1 = banana
str2 = cianaic

So the Longest Common Substring is "ana", and the length is 3.
 
Input
The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.

Process to the end of file.
 
Output
For each test case, you have to tell the length of the Longest Common Substring of them.
 
Sample Input
banana cianaic
 
Sample Output
3
 
Author
Ignatius.L

题意:给出两个字符串,求出最多可以匹配多少

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>

using namespace std;

#define N 200010

struct Sa
{
    char s[N],s1[N],s2[N];
    int rk[2][N],sa[N],h[N],w[N],now,n,len1,len2;
    int rmq[N][20],lg[N];

    bool GetS()
    {
        if(scanf("%s",s1+1)==EOF) return false;
        scanf("%s",s2+1);
        len1=strlen(s1+1);
        len2=strlen(s2+1);
        for(int i=1;i<=len1;i++) s[i]=s1[i];
        s[len1+1]='$';
        for(int i=len1+2;i<=len2+len1+2;i++) s[i]=s2[i-len1-1];
        n=len1+len2+1;
        return true;
    }

    void getsa(int z,int &m)
    {
        int x=now,y=now^=1;
        for(int i=1;i<=z;i++) rk[y][i]=n-i+1;
        for(int i=1,j=z;i<=n;i++)
            if(sa[i]>z) rk[y][++j]=sa[i]-z;
        for(int i=1;i<=m;i++) w[i]=0;
        for(int i=1;i<=n;i++) w[rk[x][rk[y][i]]]++;
        for(int i=1;i<=m;i++) w[i]+=w[i-1];
        for(int i=n;i>=1;i--) sa[w[rk[x][rk[y][i]]]--]=rk[y][i];
        for(int i=m=1;i<=n;i++)
        {
            int *a=rk[x]+sa[i],*b=rk[x]+sa[i-1];
            rk[y][sa[i]]=*a==*b&&*(a+z)==*(b+z)?m-1:m++;
        }
    }

    void getsa(int m)
    {
        now=rk[1][0]=sa[0]=s[0]=0;
        n=strlen(s+1);
        for(int i=1;i<=m;i++) w[i]=0;
        for(int i=1;i<=n;i++) w[s[i]]++;
        for(int i=1;i<=m;i++) rk[1][i]=rk[1][i-1]+(bool)w[i];
        for(int i=1;i<=m;i++) w[i]+=w[i-1];
        for(int i=1;i<=n;i++) rk[0][i]=rk[1][s[i]];
        for(int i=1;i<=n;i++) sa[w[s[i]]--]=i;
        for(int x=1,y=rk[1][m];x<=n&&y<=n;x<<=1) getsa(x,y);
        for(int i=1,j=0;i<=n;h[rk[now][i++]]=j?j--:j)
        {
            if(rk[now][i]==1) continue;
            int k=n-max(sa[rk[now][i]-1],i);
            while(j<=k&&s[sa[rk[now][i]-1]+j]==s[i+j]) ++j;
        }

    }

    void getrmq()
    {
        h[n+1]=h[1]=lg[1]=0;
        for(int i=2;i<=n;i++)
            rmq[i][0]=h[i],lg[i]=lg[i>>1]+1;
        for(int i=1;(1<<i)<=n;i++)
        {
            for(int j=2;j<=n;j++)
            {
                if(j+(1<<i)>n+1) break;
                rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<i-1)][i-1]);
            }
        }
    }

    int lcp(int x,int y)
    {
        int l=min(rk[now][x],rk[now][y])+1,r=max(rk[now][x],rk[now][y]);
        return min(rmq[l][lg[r-l+1]],rmq[r-(1<<lg[r-l+1])+1][lg[r-l+1]]);
    }

    int work()
    {
        int ans=-1;
        for(int i=2;i<=n;i++)
        {
            int MAX=max(sa[i-1],sa[i]);
            int MIN=min(sa[i-1],sa[i]);
            if(MAX>len1&&MIN<len1&&ans<h[i])
                ans=h[i];
        }
        return ans;
    }
}sa;

int main()
{
    while(sa.GetS())
    {
        sa.getsa(300);
        printf("%d\n",sa.work());
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值