• 能匹配上子串的节点对它的所有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的更多相关文章

  1. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

  2. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  3. spoj1812 LCS2 - Longest Common Substring II

    地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags  A string is fi ...

  4. 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 ...

  5. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

  6. SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】

    LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...

  7. 题解 SP1812 【LCS2 - Longest Common Substring II 】

    对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...

  8. 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, \(\ ...

  9. 【刷题】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 ...

随机推荐

  1. [转]ConcurrentHashMap原理分析

    一.背景: 线程不安全的HashMap     因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap. 效率低下的Has ...

  2. oracle之 反向键索引

    反向键索引是一种B-tree索引,它在保持列顺序的同时,物理地改变每个索引键的字节(反向键索引除了ROWID和still之外,反转每个索引列的字节).例如,如果索引键为20,如果在十六进制中存储为这个 ...

  3. webpack 4 知识点

    相应Github地址:https://github.com/cag2050/webpack4_demo css-loader 让我们能在javascript代码中导入css文件,但这还不能让css起作 ...

  4. scrapy 项目通过scrapyd部署

    年前的时候采用scrapy 爬取了某网站的数据,当时只是通过crawl 来运行了爬虫,现在还想通过持续的爬取数据所以需要把爬虫部署起来,查了下文档可以采用scrapyd来部署scrapy项目,scra ...

  5. 大数据离线分析平台 JavaSDK数据收集引擎编写

    JavaSDK设计规则 JavaSDK提供两个事件触发方法,分别为onChargeSuccess和onChargeRefund.我们在java sdk中通过一个单独的线程来发送线程数据,这样可以减少对 ...

  6. Spring学习之SpringMVC框架快速搭建实现用户登录功能

    引用自:http://blog.csdn.net/qqhjqs/article/details/41683099?utm_source=tuicool&utm_medium=referral  ...

  7. RC4被JDK8默认禁用导致腾讯QQ邮箱无法访问

    7月29日开始,腾讯修改了邮箱的加密方式,导致我们线上的所有的腾讯代收.代发邮件的功能全部失效.解决方法在最后,如果需要可直接跳转至解决方法一节 问题出现 7月29日开始,线上的所有的腾讯代收.代发邮 ...

  8. 通过IOCTL_ATA_PASS_THROUGH访问ATA设备接口

    控制代码功能:像ATA硬盘发送ATA指令.IDE/ATA:接口,一个串行,一个并行,一般叫做IDE接口的硬盘和ATA接口的硬盘.ATA指令:可以操作ATA硬盘的指令. typedef struct _ ...

  9. eclipse中maven运行run as clean等没反应处理方式

    在jdk配置处添加参数: -Dmaven.multiModuleProjectDirectory=$MAVEN_HOME 注意:这里有一个前提就是你已经正确安装maven [在环境变量中添加MAVEN ...

  10. 关于sdk>=23的android版本权限的问题

    在SDK23也就是Android6.0.1里编写调用系统通讯录读写权限的程序,在AndroidManifest.xml中,已经配置了 <uses-permission android:name= ...