SPOJ-LCS Longest Common Substring 【后缀自动机】
题目分析:
用没出现过的字符搞拼接。搞出right树,找right集合的最小和最大。如果最小和最大分居两侧可以更新答案。
代码:
#include<bits/stdc++.h>
using namespace std; const int maxn = ; int son[maxn][],fa[maxn],maxlen[maxn],root,num,sigma = ;
int minn[maxn],maxx[maxn];// lright rright string str,ss;
int n,m;
vector<int> T[maxn]; int addnew(int dt){maxlen[++num] = dt;return num;}
int ins(char x,int p,int hhhh){
int np = addnew(maxlen[p]+); minn[np] = maxx[np] = hhhh;
while(p && !son[p][x-'a']) son[p][x-'a'] = np,p = fa[p];
if(!p){fa[np] = root; return np;}
else{
int q = son[p][x-'a'];
if(maxlen[q]==maxlen[p]+){fa[np]=q;return np;}
else{
int nq = addnew(maxlen[p]+);
for(int i=;i<sigma;i++) son[nq][i] = son[q][i];
fa[nq] = fa[q]; fa[q] = fa[np] = nq;
while(p && son[p][x-'a'] == q) son[p][x-'a'] = nq,p = fa[p];
return np;
}
}
} void dfs(int now){
if(!minn[now]) minn[now] = 1e9;
for(int i=;i<T[now].size();i++){
dfs(T[now][i]);
minn[now] = min(minn[now],minn[T[now][i]]);
maxx[now] = max(maxx[now],maxx[T[now][i]]);
}
} void work(){
int lst = addnew();root = lst;
for(int i=;i<str.length();i++)
lst = ins(str[i],lst,i+);
for(int i=;i<=num;i++) T[fa[i]].push_back(i);
dfs(); int ans = ;
for(int i=;i<=num;i++)if(minn[i]<=n&&maxx[i]>n+)ans=max(ans,maxlen[i]);
cout<<ans<<endl;
} int main(){
ios::sync_with_stdio(false);
cin.tie();
cin >> str; cin >> ss;
n = str.length(); m = ss.length();
str += ('z'+); str += ss;
work();
return ;
}
SPOJ-LCS Longest Common Substring 【后缀自动机】的更多相关文章
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
- SPOJ 1811 Longest Common Substring 后缀自动机
模板来源:http://www.neroysq.com/?p=76 思路:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html 题意就是求两个字符串 ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机
Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...
- SPOJ LCS(Longest Common Substring-后缀自动机-结点的Parent包含关系)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
- SPOJ LCS Longest Common Substring(后缀自动机)题解
题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...
- SPOJ LCS - Longest Common Substring 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...
- [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串
题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...
随机推荐
- CSS3实现全景图特效
基本代码 html代码: <div class="panorama"></div> 首先定义一些基本的样式和动画: .panorama { width: 3 ...
- Valgrind.Callgrind使用
Callgrind介绍 用来对统计程序的函数调用之间的关系, 并统计每个函数的耗时 Callgrind之所以能够发现函数调用的关系, 依赖于平台的明确返回和调用指令. 在x86和amd64平台上wor ...
- 章节九、3-Desired Capabilities介绍
一.Desired Capabilities是selenium webdrive中已经写好的一个类,我们可以通过它来告诉selenium webdrive在Desired Capabilities是什 ...
- FFmpeg AVPacket相关主要函数介绍
1.AVPacket相关函数介绍 操作AVPacket的函数大约有30个,主要分为:AVPacket的创建初始化,AVPacket中的data数据管理(clone,free,copy),AVPacke ...
- Django 数据流程图
根据学习Django并且通过几个作业,发现Django制作网站的数据流程有些比较难懂,所以制作一个数据流程图,帮助自己理解,也希望对正学习的人有所帮助! 别的不多说,上美图:
- BCP SQL导出EXCEL常见问题及解决方法;数据导出存储过程
一.‘xp_cmdshell’的启用 SQL Server阻止了对组件‘xp_cmdshell’的过程‘sys.xp_cmdshell’的访问.因为此组件已作为此服务嚣安全配置的一部分而被关 闭.系统 ...
- c/c++ linux epoll系列2 利用epoll_wait查看是否可以送信
linux epoll系列2 利用epoll_wait查看是否可以送信 write函数本来是非阻塞函数,但是当缓存区被写满后,再往缓存区里写的时候,就必须等待缓存区再次变成可写,所以这是write就变 ...
- SQLServer之创建存储过程
创建存储过程注意事项 在 SQL Server. Azure SQL Database.Azure SQL 数据仓库和并行数据库中创建 Transact-SQL 或公共语言运行时 (CLR) 存储过程 ...
- GDB调试指南-启动调试
前言 GDB(GNU Debugger)是UNIX及UNIX-like下的强大调试工具,可以调试ada, c, c++, asm, minimal, d, fortran, objective-c, ...
- redis Lua学习与坑
1.在写lua脚本往redis中添加zadd 有序集合的时候一直报 "value is not a valid float"的错误,经过查询相关资料,最后发现,是顺序写反了. 相关 ...