博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【POJ】2774 Long Long Message
阅读量:7008 次
发布时间:2019-06-28

本文共 1277 字,大约阅读时间需要 4 分钟。

【题意】给定两个字符串S和T,求最长公共子串。len<=10^5。

【算法】后缀自动机

【题解】对字符串S建SAM,然后令串T在S上跑匹配。

这是自动机最原本的功能——匹配,就是串T在SAM(S)上走,不能匹配就沿失配边走,这样得到的是T上以每个字符结尾的子串中与S的最长公共子串,取Max即是答案。

注意失配不要走到0节点处。

#include
#include
#include
using namespace std;const int maxn=100010;struct tree{
int len,fa,t[30];}t[maxn*2];int last,n,m,tot;char s[maxn],T[maxn];void insert(int c){ int np=++tot; t[np].len=t[last].len+1; int x=last; while(x&&!t[x].t[c])t[x].t[c]=np,x=t[x].fa; last=np; if(!x)t[np].fa=1;else{ int y=t[x].t[c]; if(t[y].len==t[x].len+1)t[np].fa=y;else{ int nq=++tot; t[nq]=t[y]; t[nq].len=t[x].len+1; t[nq].fa=t[y].fa;t[np].fa=t[y].fa=nq; while(x&&t[x].t[c]==y)t[x].t[c]=nq,x=t[x].fa; } }}int main(){ scanf("%s",T+1);m=strlen(T+1); last=tot=1; for(int i=1;i<=m;i++)insert(T[i]-'a'); scanf("%s",s+1);n=strlen(s+1); int ans=0,cnt=0,now=1; for(int i=1;i<=n;i++){ while(now!=1&&!t[now].t[s[i]-'a'])now=t[now].fa,cnt=t[now].len; if(t[now].t[s[i]-'a'])cnt++,now=t[now].t[s[i]-'a'];else cnt=0; ans=max(ans,cnt); } printf("%d",ans); return 0;}
View Code

 

转载于:https://www.cnblogs.com/onioncyc/p/8118783.html

你可能感兴趣的文章
模块封装代码
查看>>
《Machine Learning》(第一章)序章
查看>>
【右键禁用U盘的小技巧】
查看>>
执行sql语句后的数据处理api
查看>>
jquery $.each的用法
查看>>
Python --元组与列表的差异
查看>>
PHP TP增删改
查看>>
VMware虚拟机与主机联通及配置上网
查看>>
single-row function和muti-row function
查看>>
keepalived
查看>>
意向锁
查看>>
线性规划
查看>>
常见错误分析-笔记
查看>>
P1256 显示图像(广搜)
查看>>
MongoDB(课时29 MapReduce)
查看>>
Slurm任务调度系统部署和测试(源码)(1)
查看>>
李超树详解
查看>>
怎样才是全能的程序员?
查看>>
with as的用法
查看>>
springboot oauth 鉴权之——授权码authorization_code鉴权
查看>>