Spoj 1811 LCS - longest common substring (suffix automaton)

Portal

Title Meaning: as title

This problem is actually an application of skip fail on SAM. the matching node is at root at the beginning. As we throw it in to match different characters, we continue to skip fail until the current character is matched. If pos in the skip graph changes to - 1, that is, the initial node without any characters before jumping back to root is not added, which means mismatch, and it's good to update cnt

If a match is successful if the pos is not - 1, then we can update the cnt to len[pos] and the smaller one in cnt plus one (adding one means adding the current character, and updating to the smaller one is because it must be the smaller one with a longer match, and only the smaller one can be updated)

 

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 3e5 + 10;
inline int read()
{
    char ch=getchar();
    int i=0,f=1;
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        i=(i<<3)+(i<<1)+ch-'0';
        ch=getchar();
    }
    return i*f;
}
struct SAM
{
    static const int KN = N << 1;
    static const int KM = 27;
    int fail[KN], net[KN][KM], len[KN], cnt, root;
    int rig[KN],sum[KN];
    int newnode(int _len)
    {
        memset(net[cnt], -1, sizeof(net[cnt]));
//        fail[cnt] = -1;
        len[cnt] = _len;
        return cnt++;
    }
    void init()
    {
        cnt = 0;
        memset(fail,-1,sizeof(fail));
        root = newnode(0);
    }
    int add(int p, int x)//u--np,v---p
    {
        int np=newnode(len[p]+1);
        rig[np]=1;
        while(~p && net[p][x] == -1)    net[p][x] = np, p = fail[p];
        if(p == -1) fail[np] = root;
        else
        {
            int q = net[p][x];
            if(len[q] == len[p] + 1)    fail[np] = q;
            else
            {
                int nq = newnode(len[p] + 1);
                memcpy(net[nq], net[q], sizeof(net[q]));
                fail[nq] = fail[q];
                fail[q] = fail[np] = nq;
                while(~p && net[p][x] == q) net[p][x] = nq, p = fail[p];
            }
        }
        return np;
    }
    void build(char* s, char ch)
    {
        int now = root;
        for(int i = 0; s[i]; ++i)   now = add(now, s[i] - ch);
    }
    int ord[KN], pri[KN];
    void topo()
    {
        int maxVal=0;
        memset(pri, 0, sizeof(pri));
        for (int i = 0; i < cnt; ++i) maxVal = max(maxVal, len[i]), ++ pri[len[i]];
        for (int i = 1; i <= maxVal; ++i) pri[i] += pri[i - 1];
        for (int i = 0; i < cnt; ++i) ord[--pri[len[i]]] = i;
    }
    void gao(int k)
    {
        topo();
    }
} sam;
char s[N];
char ss[N];
int main()
{
    scanf("%s",s);
    scanf("%s",ss);
    sam.init();
    sam.build(s,'a');
    sam.topo();
    int cnt=0,pos=0,ans=0;
    for(int i=0; i<strlen(ss); i++)
    {
        while(~pos&&sam.net[pos][ss[i]-'a']==-1) pos=sam.fail[pos];
        if(pos==-1) pos=0,cnt=0;
        else cnt=min(sam.len[pos],cnt)+1,pos=sam.net[pos][ss[i]-'a'],ans=max(ans,cnt);
    }
    cout<<ans<<endl;
}

 

Posted by niroshan on Fri, 20 Dec 2019 09:49:47 -0800