后缀自动机(多个穿的最长公共子串)spoj1812

SPOJ Problem Set (classical)

1812. Longest Common Substring II

Problem code: LCS2

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 a bit harder, for some given strings, find the length of the longest common substring of them.

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

Input

The input contains at most 10 lines, each line consists of no more than 100000 lowercase letters, representing a string.

Output

The length of the longest common substring. If such string doesn't exist, print "0" instead.

Example

Input:
alsdfkjfjkdsal
fdjskalajfkdsla
aaaajfaaaa

Output:
2


原来的问题想明白了,请看注释。。。

其实就是是不是能走到的问题。

先将一个串建SAM,然后用后面的串去匹配,对于每一个串,保存最大值,对于不同的串,更新最小值。

SAM结点多两个值,ml表示多个串的最小值,nl表示当前串匹配的最大值。将SAM拓扑排序后自底向上更新,设当前状态为p,如果p->ml>p->nl,则p->ml=p->nl,如果p有父亲,设q为p的父亲,若p->nl>q->nl,则由SAM的性质可知,p的最大匹配数可以向上传递给它的父亲

从大神的博客上看到一句话,感觉挺有用的:出现次数向父亲传递,接收串数从儿子获取

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1000100;
const int SIGMA_SIZE=26;
struct SAM_Node
{
    SAM_Node *par,*next[SIGMA_SIZE];
    int len,id,pos;
    int nl,ml;
    SAM_Node(){}
    SAM_Node(int _len)
    {
        par=0;
        len=_len;
        nl=0;
        ml=len;
        memset(next,0,sizeof(next));
    }
};
SAM_Node node[maxn*2],*root,*last;
int SAM_size;
SAM_Node *newSAM_Node(int len)
{
    node[SAM_size]=SAM_Node(len);
    node[SAM_size].id=SAM_size;
    return &node[SAM_size++];
}
SAM_Node *newSAM_Node(SAM_Node *p)
{
    node[SAM_size]=*p;
    node[SAM_size].id=SAM_size;
    return &node[SAM_size++];
}
void SAM_add(int x,int len)
{
    SAM_Node *p=last,*np=newSAM_Node(p->len+1);
    last=np;
    while(p&&!p->next[x])
    {
        p->next[x]=np;
        p=p->par;
    }
    if(!p)
        np->par=root;
    else
    {
        SAM_Node *q=p->next[x];
        if(q->len==p->len+1)
            np->par=q;
        else
        {
            SAM_Node *nq=newSAM_Node(q);
            nq->len=nq->ml=p->len+1;
            q->par=nq;
            np->par=nq;
            while(p&&p->next[x]==q)
            {
                p->next[x]=nq;
                p=p->par;
            }
        }
    }
}
void SAM_init()
{
    SAM_size=0;
    root=last=newSAM_Node(0);
    node[0].pos=0;
}
void SAM_build(char *s)
{
    SAM_init();
    int len=strlen(s);
    for(int i=0;i<len;i++)
        SAM_add(s[i]-'a',i+1);
}
int cnt[maxn*2];
char s[maxn];
SAM_Node *sa[maxn*2];
int main()
{
    scanf("%s",s);
    SAM_build(s);
    int len=strlen(s);
    for(int i=0;i<=len;i++)cnt[i]=0;
    for(int i=0;i<SAM_size;i++)cnt[node[i].len]++;
    for(int i=1;i<=len;i++)cnt[i]+=cnt[i-1];
    for(int i=SAM_size-1;i>=0;i--)sa[--cnt[node[i].len]]=&node[i];
    while(scanf("%s",s)!=EOF)
    {
        len=strlen(s);
        SAM_Node *p=root;
        int num=0;
        for(int i=0;i<len;i++)
        {
            int j=s[i]-'a';
            if(p->next[j]){num++;p=p->next[j];}
            else
            {
                while(p&&!p->next[j])p=p->par;
                if(p){num=p->len+1;p=p->next[j];}
                else num=0,p=root;
            }
            p->nl=max(num,p->nl);
        }
        for(int i=SAM_size-1;i>=0;i--)
        {
            p=sa[i];
            if(p->nl<p->ml)p->ml=p->nl;
            //好像想明白了,为什么要比较p->par->nl和p->nl了
            //因为子节点的后缀长度要比父节点的长,有可能前面比父节点长的那部分没有匹配
            //导致父节点的匹配长度比子节点长度长,反过来直接从子节点多出来的部分开始匹配
            //没有走到父节点的话,父节点的匹配长度就小了,所以要更新
            if(p->par&&p->par->nl<p->nl)
                p->par->nl=p->nl;
            p->nl=0;
        }
    }
    int ans=0;
    for(int i=0;i<SAM_size;i++)
        if(node[i].ml>ans)ans=node[i].ml;
    printf("%d\n",ans);
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值