[后缀自动机 模板题] SPOJ 1811Longest Common Substring
来源:互联网 发布:中兴证券软件下载 编辑:程序博客网 时间:2024/05/17 14:15
题目大意:两个串的最长公共子串
后缀自动机模板题啦
对A建自动机 B在上面匹配
假设现在到B[i-1]的最长公共子串长度为l
如果当前节点有B[i]这个孩子,那么直接就l++即可。
如果没有就找一直向前找suffix_link,直到找到有B[i]这个孩子的节点
类似KMP中的next和AC自动机中的fail
#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++;}inline int read(char *s){ char c=nc(); int len=0; for (;!(c>='a' && c<='z');c=nc()); for (;c>='a' && c<='z';s[++len]=c,c=nc()); s[++len]=0; return len-1;}const int N=250005;struct state{ int len,link,next[26];}st[N<<1];int ncnt,last;inline void extend(char c){ int cur=++ncnt,p; c-='a'; st[cur].len=st[last].len+1; for (p=last;p!=-1 && !st[p].next[c];p=st[p].link) st[p].next[c]=cur; if (p==-1) st[cur].link=0; else{ int q=st[p].next[c]; if (st[q].len==st[p].len+1) st[cur].link=q; else{ int nq=++ncnt; st[nq].len=st[p].len+1; st[nq].link=st[q].link; for (int i=0;i<26;i++) st[nq].next[i]=st[q].next[i]; for (;p!=-1 && st[p].next[c]==q;p=st[p].link) st[p].next[c]=nq; st[q].link=st[cur].link=nq; } } last=cur;}char A[N],B[N];int na,nb;int main(){ freopen("t.in","r",stdin); freopen("t.out","w",stdout); na=read(A); nb=read(B); st[0].link=-1; for (int i=1;i<=na;i++) extend(A[i]); int p=0,l=0,maxv=0; for (int i=1;i<=nb;i++){ while (p && !st[p].next[B[i]-'a']) p=st[p].link,l=st[p].len; if (st[p].next[B[i]-'a']) p=st[p].next[B[i]-'a'],l++; maxv=max(maxv,l); } printf("%d\n",maxv); return 0;}
0 0
- [后缀自动机 模板题] SPOJ 1811Longest Common Substring
- spoj 1811 Longest Common Substring (后缀自动机)
- SPOJ-1811 Longest Common Substring(后缀自动机)
- SPOJ LCS Longest Common Substring 后缀自动机
- [SPOJ LCS]Longest Common Substring && 后缀自动机
- 【后缀自动机】[SPOJ LCS]Longest Common Substring
- SPOJ 1811 Longest Common Substring(后缀自动机)
- spoj 1811 Longest Common Substring (第一道后缀自动机)
- spoj 1811 LCS - Longest Common Substring (后缀自动机)
- spoj 1811 Longest Common Substring(后缀自动机)
- [后缀自动机 模板题] SPOJ 1812Longest Common Substring II & BZOJ 2946 [Poi2000]公共串
- SPOJ 1812. Longest Common Substring II(后缀自动机)
- SPOJ LCS2 Longest Common Substring II 后缀自动机
- 【后缀自动机】[SPOJ LCS2]Longest Common Substring II
- spoj 1812 LCS2 - Longest Common Substring II (后缀自动机)
- SPOJ lcs2 Longest Common Substring II 后缀自动机
- spoj 1812 Longest Common Substring II(后缀自动机)
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机)
- Javascript的基本数据类型
- Matlab编程与数据类型 -- 分支条件选择语句if/end
- react-native在Anroid真机运行时可能会遇到白屏的情况解决办法
- java学习笔记(高琪版)----抽象类
- 增强学习系列之(二):实现一个简单的增强学习的例子
- [后缀自动机 模板题] SPOJ 1811Longest Common Substring
- 背景background
- FPGA 学习之路 (七) UART串口设计
- 快速幂以及斐波那契数列的运用
- runtime总结二之消息机制(包括消息转发,消息交换的黑魔法)
- react-native在Anroid真机运行时可能会遇到白屏的情况解决办法
- 端口号、IP、网关的作用
- 【1月英语—罗塞塔之爱】
- LeetCode_463. Island Perimeter