• 能匹配上子串的节点对它的所有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. Qt开发问答

    Qt开发问答 1, Difference between Dialog and widget and QMainWindow http://www.qtcentre.org/threads/3465- ...

  2. focus、input、keydown、keyup、change、blur方法的差异

    focus.input.keydown.keyup.change.blur方法的差异: https://blog.csdn.net/yiifaa/article/details/52372022 bl ...

  3. create-react-app 知识点

    用 create-react-app 创建项目时,最好FQ:否则有些安装包会安装不上,报超时错误. create-react-app 创建的项目,执行yarn eject后,按需引入antd的2种方式 ...

  4. Fixed-point multiplication (C166 A*B/B)

    I want to multiply two fixed point numbers. After the multiplication I have to shift the result so t ...

  5. HanLP的自定义词典使用方式与注意事项介绍

    [环境]python 2.7 方法一:使用pyhanlp,具体方法如下: pip install pyhanlp  # 安装pyhanlp 进入python安装包路径,如 /usr/lib/pytho ...

  6. 脱壳:OEP(即程序入口点)查找 --- 基本思路和常见方法

    OEP:程序的入口点,软件加壳就是隐藏了OEP(或者用了假的OEP), 只要我们找到程序真正的OEP,就可以立刻脱壳. PUSHAD (压栈) 代表程序的入口点, POPAD (出栈) 代表程序的出口 ...

  7. centos7 svn服务器的搭建

    centos7下svn的安装与配置   1.环境 centos7 2.安装svnyum -y install subversion 3.配置 建立版本库目录mkdir /www/svndata svn ...

  8. 关于java的一些小知识(课程作业01)

    1,在java源代码中加空格注释不会影响程序的编译速度. 2,一个java类文件中真的只能有一个公有类吗? 如果只在第一个public类里面定义实体,或者两个都不定义并没有报错且程序可以运行.每个编译 ...

  9. Styles in Windows Phone

    1. Use resources in internal style file: <Application.Resources> <ResourceDictionary Source ...

  10. url参数的转码和解码

    encodeURI(str) //转码 decodeURI(str)//解码