SPOJ LCS2 后缀自动机

题意:
求多个串的LCS
思路:
跟上题一样的东西….
只不过多加一个临时的dp数组来保存所有的状态的最小值
然后求所有子串的最大值就好了

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long LL;
const int maxn = 100005;
const int inf=(1<<28)-1;
struct sam_node
{
    int fa,son[26];
    int len;
    void init(int _len)
    {
        len=_len;fa=-1;
        memset(son,-1,sizeof(son));
    }
};
class SAM
{
    public:
    int root,last,tots;
    sam_node sam[maxn*2];//length*2
    SAM()
    {
        init();
    }
    void init()
    {
        tots=0;
        root=last=0;
        sam[tots].init(0);
    }
    void extend(char ch)
    {
        int w=ch-'a';
        int p=last;
        int np=++tots;sam[tots].init(sam[p].len+1);
        int q,nq;while(p!=-1&&sam[p].son[w]==-1)
        sam[p].son[w]=np,p=sam[p].fa;
        if (p==-1) sam[np].fa=root;
        else
        {
            q=sam[p].son[w];
            if (sam[p].len+1==sam[q].len) sam[np].fa=q;
            else
            {
                nq=++tots;sam[nq].init(0);
                sam[nq]=sam[q];
                sam[nq].len=sam[p].len+1;
                sam[q].fa=nq;sam[np].fa=nq;
                while(p!=-1&&sam[p].son[w]==q)
                sam[p].son[w]=nq,p=sam[p].fa;
            }
        }
        last=np;
    }
    void build(char* str)
    {
        int len=strlen(str);
        for(int i=0;i<len;++i) extend(str[i]);
    }
    int r[maxn*2],w[maxn];
    //r[i]为topsort后 入度为0先入队 
    void topsort()
    {
        int i,len=tots;
        memset(w,0,sizeof(w));
        for(int i=1;i<=tots;++i) w[sam[i].len]++;
        for(int i=1;i<=len;++i) w[i]+=w[i-1];
        for(int i=tots;i>=1;--i) r[w[sam[i].len]--]=i;
        r[0]=0;
    }
    void output()
    {
        for(int i=0;i<=tots;++i)
        {
            printf("%d(fa=%d): ",i,sam[i].fa);
            for(int j=0;j<26;++j)
            if(sam[i].son[j]!=-1)
            printf("%c(%d) ",j+'a',sam[i].son[j]);
            printf("\n");
        }
    }
    int dp[maxn*2];
    void fun(char* str)
    {
        topsort();
        int len=strlen(str),p=root,tmp=0;
        for(int i=0;i<len;++i)
        {
            int id=str[i]-'a';
            p=sam[p].son[id];
            tmp++;
            dp[p]=tmp;
        }
        for(int i=tots;i>=1;--i)
        {
            int p=r[i];
            int pre=sam[p].fa;
            dp[pre]=max(dp[pre],dp[p]);
            dp[pre]=min(dp[pre],sam[pre].len);
        }
    }
    int Cnt[maxn*2];
    void Add(char* str)
    {
        memset(Cnt,0,sizeof(Cnt));
        int len=strlen(str),p=root,tmp=0;
        for(int i=0;i<len;++i)
        {
            int id=str[i]-'a';
            while(sam[p].fa!=-1&&sam[p].son[id]==-1)
            {
                p=sam[p].fa;
                tmp=sam[p].len;
            }
            if(sam[p].son[id]==-1)
            {
                tmp=sam[p].len;
            }
            else
            {
                tmp++;
                p=sam[p].son[id];
            }
            Cnt[p]=max(Cnt[p],tmp);
        }
        for(int i=tots;i>=1;--i)
        {
            p=r[i];
            int pre=sam[p].fa;
            Cnt[pre]=max(Cnt[pre],Cnt[p]);
            Cnt[pre]=min(Cnt[pre],sam[pre].len);
            dp[p]=min(dp[p],Cnt[p]);
        }
    }
    void Solve()
    {
        int Ans=0;
        for(int i=1;i<=tots;++i)
            Ans=max(Ans,dp[r[i]]);
        printf("%d\n",Ans);
    }
}Sam;
char str[maxn];
int main()
{
    scanf("%s",str);
    int len=strlen(str);
    for(int i=0;i<len;++i)
        Sam.extend(str[i]);
    Sam.fun(str);
    while(~scanf("%s",str))
        Sam.Add(str);
    Sam.Solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值