刷题总结:最长公共字串(spoj1811)(后缀自动机)
2024-08-29 18:19:34
题目:
就不贴了吧···如题;
题解:
后缀自动机模版题;没啥好说的····
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=;
int pre[N],step[N],son[N][],tot=,last=,root=;
char s[N];
struct suffix_auto
{
void extend(int ch)
{
int p=last,np=++tot;
step[tot]=step[last]+;
for(;p&&!son[p][ch];p=pre[p]) son[p][ch]=np;
if(!p) pre[np]=root;
else
{
int q=son[p][ch];
if(step[q]!=step[p]+)
{
int nq=++tot;
step[nq]=step[p]+;
memcpy(son[nq],son[q],sizeof(son[q]));
pre[nq]=pre[q];
pre[np]=pre[q]=nq;
for(;son[p][ch]==q;p=pre[p]) son[p][ch]=nq;
}
else
pre[np]=q;
}
last=np;
}
void build()
{
scanf("%s",s);
int len=strlen(s);
for(int i=;i<len;i++)
extend(s[i]-'A');
}
}automaton;
int main()
{
automaton.build();
scanf("%s",s);
int n=strlen(s);
int ans=,len=,p=;
for(int i=;i<n;i++)
{
int ch=s[i]-'A';
if(son[p][ch]) p=son[p][ch],len++;
else
{
while(p&&!son[p][ch]) p=pre[p];
if(!p) len=,p=root;
else
len=step[p]+,p=son[p][ch];
}
ans=max(ans,len);
}
cout<<ans<<endl;
return ;
}
最新文章
- 北京54全国80及WGS84坐标系的相互转换
- angularJs指令深度分析
- [javaSE] 反射-动态加载类
- php 通过API接口连接12306余票查询
- C++静态成员和静态成员函数
- servlet 生命周期
- 剑指架构师系列-Hibernate需要掌握的Annotation
- centos6.6安装redis服务安装redis服务,对于discuz来说可以作为缓存使用,减轻服务器压力
- 针对安卓java入门:数据类型
- HDOJ2015偶数求和
- 详解浏览器缓存机制与Apache设置缓存
- Journal.Today 1.0.0
- css3图片滤镜
- 五分钟学会centos配置gitlab
- mariadb笔记
- HTTP协议02-请求和响应的报文构成
- python3 读取dbf文件报错 UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode
- python 读取excel数据
- BinaryReader 自己写序列化
- 各种版本的ST-LINK仿真器
热门文章
- nmon安装和使用介绍
- hihoCoder #1080 : 更为复杂的买卖房屋姿势 (线段树,多tag)
- COGS 2566. [51nod 1129] 字符串最大值
- codevs 2776 寻找代表元
- maven打包错误:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
- UVA1660 Cable TV Network (无向图的点连通度)
- codeforces Gym 100338E 	Numbers (贪心,实现)
- SVN中的check out与export的区别
- C03 程序逻辑
- quartz测试类