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 ...
随机推荐
- 用express快速写一个hello world
首页要具备 node.js环境, npm环境 创建一个目录, 然后进入此目录作为工作目录 mkdir myapp cd myapp 通过npm init 创建一个package.json文件 npm ...
- js正则表达式只能是数字、字母或下划线
//只能是数字.字母或下划线 function isValid(str) { var reg = /^\w+$/g; return reg.test(str); }
- 在Ubuntu上使用noip动态域名的方法(ddns)
首先,注册一个noip.com的帐号. 注册的步骤见这篇教程:http://www.cnblogs.com/infopi/p/3991407.html 建立目录 第1行进入当前用户的home目录 第2 ...
- CoreException: Could not get the value for parameter compilerId for plugin execution default-compile Maven项目pom文件报错,插件引用不到
CoreException: Could not get the value for parameter compilerId for plugin execution default-compile ...
- IDEA中使用springBoot+gradle构建多模块项目
https://blog.csdn.net/forMelo/article/details/78995875
- Spring Cloud 快速教程
官方:http://projects.spring.io/spring-cloud/ 中文:https://springcloud.cc/ https://blog.csdn.net/forezp/a ...
- Mibew Messenger (also known as Open Web Messenger)
Mibew Messenger (also known as Open Web Messenger) is an open-source live support application writte ...
- zookeeper选举状态介绍 摘自https://cloud.tencent.com/developer/news/303891
zookeeper集群 配置多个实例共同构成一个集群对外提供服务以达到水平扩展的目的,每个服务器上的数据是相同的,每一个服务器均可以对外提供读和写的服务,这点和redis是相同的,即对客户端来讲每个服 ...
- Python数据库编程
http://lizhenliang.blog.51cto.com/7876557/1874283 http://blog.itpub.net/22664653/list/1/?cid=86471
- Django中的URL映射
1.为什么回去urls.py文件中寻找映射呢? 因为在settings.py中配置了ROOT_URLCONF为urls.py: ROOT_URLCONF = 'first_project.urls' ...