SP1812 LCS2 - Longest Common Substring II
- 能匹配上子串的节点对它的所有parent都有贡献
- 在树上转移即可
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define ll long long
#define M 200010
using namespace std;
int read()
{
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
int ch[M][26], len[M], fa[M], tim[M], a[M], cnt = 1, lst = 1, dp[M], ans[M];
char s[M];
void insert(int c)
{
int p = ++cnt, f = lst;
lst = p;
len[p] = len[f] + 1;
while(f && !ch[f][c]) ch[f][c] = p, f = fa[f];
if(!f) fa[p] = 1;
else
{
int q = ch[f][c];
if(len[q] == len[f] + 1) fa[p] = q;
else
{
int nq = ++cnt;
fa[nq] = fa[q];
memcpy(ch[nq], ch[q], sizeof(ch[nq]));
len[nq] = len[f] + 1;
fa[q] = fa[p] = nq;
while(f && ch[f][c] == q) ch[f][c] = nq, f = fa[f];
}
}
}
int main()
{
scanf("%s", s + 1);
int l = strlen(s + 1);
for(int i = 1; i <= l; i++) insert(s[i] - 'a');
for(int i = 1; i <= cnt; i++) tim[len[i]]++;
for(int i = 1; i <= cnt; i++) tim[i] += tim[i - 1];
for(int i = 1; i <= cnt; i++) a[tim[len[i]]--] = i;
for(int i = 1; i <= cnt; i++) ans[i] = len[i];
while(scanf("%s", s + 1) != EOF)
{
memset(dp, 0, sizeof(dp));
l = strlen(s + 1);
int now = 1, tt = 0;
for(int i = 1; i <= l; i++)
{
int c = s[i] - 'a';
if(ch[now][c])
{
tt++, now = ch[now][c];
}
else
{
while(now && !ch[now][c]) now = fa[now];
if(!now) now = 1, tt = 0;
else tt = len[now] + 1, now = ch[now][c];
}
dp[now] = max(dp[now], tt);
}
for(int i = cnt; i >= 0; i--) dp[fa[i]] = max(dp[fa[i]], dp[i]);
for(int i = 1; i <= cnt; i++) ans[i] = min(ans[i], dp[i]);
}
for(int i = 1; i <= cnt; i++) ans[i] = max(ans[i], ans[i - 1]);
cout << ans[cnt] << "\n";
return 0;
}
SP1812 LCS2 - Longest Common Substring II的更多相关文章
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- spoj1812 LCS2 - Longest Common Substring II
地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags A string is fi ...
- SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS
LCS2 - Longest Common Substring II no tags A string is finite sequence of characters over a non-emp ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】
LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...
- 题解 SP1812 【LCS2 - Longest Common Substring II 】
对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...
- LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
- 【刷题】SPOJ 1812 LCS2 - Longest Common Substring II
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
随机推荐
- [转]JDK自带工具之问题排查场景示例
最近看到了大量关于java性能调优.故障排查的文章,自己也写了一篇Java调优经验谈.接着此篇文章,其实一直打算写写一些常用调优工具以及它们的惯常用法的.后来在http://java-performa ...
- [转]DB2错误代码大全
DB2 SQLSTATE 消息 异常 一2008-03-31 13:17SQLSTATE 消息本节列示 SQLSTATE 及其含义.SQLSTATE 是按类代码进行分组的:对于子代码,请参阅相应的表. ...
- node api 之:Error
Node.js 中运行的应用程序一般会遇到以下四类错误: 标准的 JavaScript 错误: <EvalError> : 当调用 eval() 失败时抛出. <SyntaxErro ...
- Nunjucks:Mozilla 开发的 JavaScript 模板引擎
Nunjucks 中文网站:https://nunjucks.bootcss.com/
- R语言入门
引入R的package(库) 首先是要安装TSA库,TSA是作者自己开发的一套基于R的pacakge,里面包含了函数以及数据:安装的方式是在R的控制台(console)中敲入install.packa ...
- ElasticeSearch(五)分布式索引架构
关于分布式架构 首先将ES默认每个索引是5个分片,这样做得目的是两个一个索引的时候速度更快(将数据写到小分片的尾部比写入大分片尾部更加快):另外一个是当数据量达到一定程度之后,分片查询,在汇总(sca ...
- [转]Aroon Indicator
Aroon Indicator Trend Oscillator Description The Aroon indicator, developed by Tushar Chande, indica ...
- gtid error set test
1.从库报主键重复(Errno: 1062)#create test data 1062create table t1 (id tinyint not null primary key,ename v ...
- 微信开发-业务域名、JS接口安全域名、网页授权域名
在微信公众平台上可配置这些域名. 1.业务域名:在微信浏览器中点击文本框,会弹出下面的提示,很不爽,通过配置业务域名可以将该提示去掉 2.JS接口安全域名:分享到朋友圈(js-sdk)时用上,此接口要 ...
- 跟着未名学 - 录屏套件 Camtasia Studio
目录 Camtasia Recorder. 1 Camtasia Studio.. 2 时间线... 2 渲染... 5 Camtasia MenuMaker. 6 Camtasia Play. 6 ...