spoj LCS 后缀自动机

链接:http://www.spoj.com/problems/LCS/

题意两串LCS

确实没什么好说的,第一次编嘛。把网上的教程斗翻出来看一遍就好了。

另发现,百度内部用户交流使用的图片在百度快照中看得到。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 2510000
#define MAXT MAXN*2
struct SAM_node
{
        int pnt,len;
        int nxt[26];
        void Print(int id=-1)
        {
                printf("-----------------\n");
                printf("Sam id:%d\n",id);
                printf("Next :\n");
                for (int i=0;i<26;i++)
                        printf("[%c:%d];",i+'a',nxt[i]);
                printf("\n");
                printf("Parent:%d\n",pnt);
                printf("Length:%d\n",len);
        }
}sam[MAXT];
int last=1;
int topt=1;
void Add_item(char ch)
{
        int p,np;
        p=last;
        np=++topt;
        sam[np].len=sam[p].len+1;
        while (p && !sam[p].nxt[ch-'a'])
                sam[p].nxt[ch-'a']=np,p=sam[p].pnt;
        if (!p)
        {
                sam[np].pnt=1;
                last=np;
        }else
        {
                int q=sam[p].nxt[ch-'a'];
                if (sam[q].len==sam[p].len+1)
                        sam[np].pnt=q;
                else
                {
                        int nq;
                        nq=++topt;
                        sam[nq]=sam[q];
                        sam[nq].len=sam[p].len+1;
                        sam[nq].pnt=sam[q].pnt;
                        sam[q].pnt=nq;
                        sam[np].pnt=nq;
                        while (p && sam[p].nxt[ch-'a']==q)
                        {
                                sam[p].nxt[ch-'a']=nq;
                                p=sam[p].pnt;
                        }
                }
                last=np;
        }
}


char str[MAXN];
char str2[MAXN];
int main()
{
        freopen("input.txt","r",stdin);
        scanf("%s",str);
        int n=strlen(str);
        for (int i=0;i<n;i++)
                Add_item(str[i]);
        for (int i=0;i<=topt;i++)
        {
                //sam[i].Print(i);
        }
        scanf("%s",str2);
        int now=1,res=0,ans=0;
        int m=strlen(str2);
        for (int i=0;i<m;i++)
        {
                if (sam[now].nxt[str2[i]-'a'])
                {
                        now=sam[now].nxt[str2[i]-'a'];
                        res++;
                        ans=max(ans,res);
                }else
                {
                        while (now && !sam[now].nxt[str2[i]-'a'])
                                now=sam[now].pnt;
                        if (!now)
                        {
                                now=1;
                                res=0;
                        }
                        else
                        {
                                res=sam[now].len+1;
                                now=sam[now].nxt[str2[i]-'a'];
                                ans=max(ans,res);
                        }
                }
        }
        printf("%d\n",ans);
}

 

转载于:https://www.cnblogs.com/mhy12345/p/4430010.html

微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值