SPOJ1811 Longest Common Substring

题意

A string is finite sequence of characters over a non-empty finite set Σ.

In this problem, Σ is the set of lowercase letters.

Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.

Now your task is simple, for two given strings, find the length of the longest common substring of them.

Here common substring means a substring of two or more strings.

给两个串A和B,求这两个串的最长公共子串。

no more than 250000

分析

参照OI wiki

给定两个字符串 S 和 T ,求出最长公共子串,公共子串定义为在 S 和 T 中 都作为子串出现过的字符串 X 。

我们为字符串 S 构造后缀自动机。

我们现在处理字符串 T ,对于每一个前缀都在 S 中寻找这个前缀的最长后缀。换句话 说,对于每个字符串 T 中的位置,我们想要找到这个位置结束的 S 和 T 的最长公 共子串的长度。

为了达到这一目的,我们使用两个变量,当前状态 v 和 当前长度 l 。这两 个变量描述当前匹配的部分:它的长度和它们对应的状态。

一开始 v=t_0且 l=0 ,即,匹配为空串。

现在我们来描述如何添加一个字符 T[i] 并为其重新计算答案:

如果存在一个从 v 到字符 T[i] 的转移,我们只需要转移并让 l 自增一。
如果不存在这样的转移,我们需要缩短当前匹配的部分,这意味着我们需要按照以下后 缀链接进行转移:

v=link(v)

与此同时,需要缩短当前长度。显然我们需要将 l 赋值为 len(v) ,因为经过这个后缀链接后我们到达的状态所对应的最长字符串是一个子串。

如果仍然没有使用这一字符的转移,我们继续重复经过后缀链接并减小 l ,直到我们 找到一个转移或到达虚拟状态 -1 (这意味着字符 T[i] 根本没有在 S 中出现过, 所以我们设置 v=l=0 )。

问题的答案就是所有 l 的最大值。

这一部分的时间复杂度为 O(length(T)) ,因为每次移动我们要么可以使 l 增加一, 要么可以在后缀链接间移动几次,每次都减小 l 的值。

时间复杂度\(O(|S| + |T|)\)

代码

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<typename T>il T read()
{
    rg T data=0,w=1;
    rg char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        data=data*10+ch-'0';
        ch=getchar();
    }
    return data*w;
}
template<typename T>il T read(rg T&x)
{
    return x=read<T>();
}
typedef long long ll;

co int N=5e5;
namespace SAM
{
    int tot,last;
    int ch[N][26],fail[N]={-1},len[N];
    void extend(int k)
    {
        int cur=++tot;
        len[cur]=len[last]+1;
        int p=last;
        while(~p&&!ch[p][k])
        {
            ch[p][k]=cur;
            p=fail[p];
        }
        if(p==-1)
            fail[cur]=0;
        else
        {
            int q=ch[p][k];
            if(len[q]==len[p]+1)
                fail[cur]=q;
            else
            {
                int clone=++tot;
                std::copy(ch[q],ch[q]+26,ch[clone]);
                fail[clone]=fail[q],len[clone]=len[p]+1;
                while(~p&&ch[p][k]==q)
                {
                    ch[p][k]=clone;
                    p=fail[p];
                }
                fail[cur]=fail[q]=clone;
            }
        }
        last=cur;
    }
    void ins(char s[],int n)
    {
        for(int i=0;i<n;++i)
            extend(s[i]-'a');
    }
    void solve(char s[],int n)
    {
        int ans=0,v=0,l=0;
        for(int i=0;i<n;++i)
        {
            int k=s[i]-'a';
            if(ch[v][k])
                v=ch[v][k],++l;
            else
            {
                while(~v&&!ch[v][k])
                    v=fail[v];
                if(v==-1)
                    v=l=0;
                else
                    l=len[v]+1,v=ch[v][k];
            }
            ans=std::max(ans,l);
        }
        printf("%d\n",ans);
    }
}
char buf[N];

int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    scanf("%s",buf);
    SAM::ins(buf,strlen(buf));
    scanf("%s",buf);
    SAM::solve(buf,strlen(buf));
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/autoint/p/10345276.html