[POI 2000] 公共串
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=2946
[算法]
建立多串后缀树
对于后缀树上的每个点 , 判断该节点所代表的等价类是否在所以字符串中出现 , 用该点的深度更新答案
时间复杂度 : O(NL)
[代码]
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e5 + ;
const int ALPHA = ; int n , ans;
char s[N]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} struct Suffix_Automaton
{
int sz , last;
int father[N] , child[N][ALPHA] , depth[N];
bool lab[N][];
vector< int > a[N];
Suffix_Automaton()
{
sz = ;
last = ;
}
inline int new_node(int dep)
{
depth[++sz] = dep;
father[sz] = ;
memset(child[sz] , , sizeof(child[sz]));
memset(lab[sz] , , sizeof(lab[sz]));
return sz;
}
inline void extend(int ch , int c)
{
int np = child[last][ch];
if (np)
{
if (depth[np] == depth[last] + )
{
lab[np][c] = true;
last = np;
} else
{
int nq = new_node(depth[last] + );
father[nq] = father[np];
father[np] = nq;
memcpy(child[nq] , child[np] , sizeof(child[nq]));
for (int p = last; child[p][ch] == np; p = father[p])
child[p][ch] = nq;
lab[nq][c] = true;
last = nq;
}
} else
{
int np = new_node(depth[last] + );
int p = last;
while (child[p][ch] == )
{
child[p][ch] = np;
p = father[p];
}
if (child[p][ch] == np)
{
father[np] = ;
lab[np][c] = true;
last = np;
return;
}
int q = child[p][ch];
if (depth[q] == depth[p] + )
{
father[np] = q;
lab[np][c] = true;
last = np;
return;
} else
{
int nq = new_node(depth[p] + );
father[nq] = father[q];
father[np] = father[q] = nq;
memcpy(child[nq] , child[q] , sizeof(child[q]));
while (child[p][ch] == q)
{
child[p][ch] = nq;
p = father[p];
}
lab[np][c] = true;
last = np;
return;
}
}
}
inline void insert(char *s , int col)
{
last = ;
for (int i = ; i <= strlen(s + ); ++i)
extend(s[i] - 'a' , col);
}
inline void dfs(int u)
{
for (unsigned i = ; i < a[u].size(); ++i)
{
int v = a[u][i];
dfs(v);
for (int j = ; j <= n; ++j)
lab[u][j] |= lab[v][j];
}
bool all = true;
for (int i = ; i <= n; ++i) all &= lab[u][i];
if (all) chkmax(ans , depth[u]);
}
inline void work()
{
for (int i = ; i <= sz; ++i)
a[father[i]].push_back(i);
dfs();
}
} SAM; int main()
{ read(n);
for (int i = ; i <= n; ++i)
{
scanf("%s" , s + );
SAM.insert(s , i);
}
SAM.work();
printf("%d\n" , ans); return ; }
[POI 2000] 公共串的更多相关文章
- BZOJ 2946: [Poi2000]公共串
2946: [Poi2000]公共串 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 787 Solved: 342[Submit][Status][D ...
- BZOJ 2946: [Poi2000]公共串( 后缀自动机 )
一个串建后缀自动机, 其他串在上面跑, 然后用当前串跑的去更新全部 ------------------------------------------------------------------ ...
- 【BZOJ2946】公共串(后缀数组)
[BZOJ2946]公共串(后缀数组) 题面 权限题... 只有CJOJ题面啦 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: 读入单词,计算最长公共子串的 ...
- BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案
BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单 ...
- 【BZOJ】【2946】【POI2000】公共串
后缀数组 好感动,复习了下后缀数组居然写出来了……(感谢ykz大神) 求最长公共子串……WA了一发是因为:[不同字符串之间要用不同的特殊字符隔开]否则就会匹配到相同→_→比如都是aaa结尾,如果用相同 ...
- 【BZOJ2946】[Poi2000]公共串 后缀数组+二分
[BZOJ2946][Poi2000]公共串 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单词 l 计 ...
- 【BZOJ 2946】 2946: [Poi2000]公共串 (SAM)
2946: [Poi2000]公共串 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1063 Solved: 469 Description ...
- 【BZOJ2946】公共串 [SAM]
公共串 Time Limit: 3 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 给出几个由小写字母构成的单词,求它们最 ...
- [BZOJ2946] [Poi2000]公共串解题报告|后缀数组
给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 单词个数<=5,每个单词长度<=2000 尽管最近在学的是SAM...但是看到这个题还是忍不住想写SA... (其实是不 ...
随机推荐
- ubuntu16.04下Cmake学习一
根据网上的资料,我总结了一下,一个工程应该有根目录(bin)存放可执行文件,头文件目录(include)存放头文件,源码文件(src)存放你的算法,还需要一个库文件夹存放你编译的静态库或者动态库.然后 ...
- iOS项目 -- 模仿花椒直播做的第三层设计完整版
由于是获取第三方的数据,开发的时候,把数据结构分为:闭环数据,和开环数据. 开环数据是网络的第三方数据,自己不能控制的了. 闭环数据是自己的数据,可以进行各式各样的设计. 这是闭环数据的数据库关键字设 ...
- ClassNotFoundException Log
Studio 运行时异常: Error:Execution failed for task ':app:compileDebugJavaWithJavac'.> Compilation fail ...
- python 基础 6.0 异常的常用形式
一. 异常 异常既是一个时间,该事件会在程序执行过程中发生,影响了程序的正常执行.一般情况下,在python无法正常处理程序时就会发生一个异常.异常是python对象,表示一个错误.当python ...
- 使用EasyPusher进行手机低延时直播推流便捷开发
基于EasyPusher sdk库工程(即library module)实现一个推送客户端非常简单便捷,因为sdk端已经将各种烦人的状态维护\错误检查\权限判定\UI同步等功能都实现了,开发者仅仅只需 ...
- Upgrading Elasticsearch
Upgrading Elasticsearch | Elasticsearch Reference [5.6] | Elastic https://www.elastic.co/guide/en/el ...
- linux c编程:gdb的使用
首先用一个简单的打印字符的程序来做下示例 #include<stdio.h>#include<string.h>void main(){ int i=0; char ...
- 怎样做大做强企业中的ERP?
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luozhonghua2014/article/details/37672409 ...
- 公司IIS 项目公布 注意点
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/sat472291519/article/details/24010721 IIS - 右键 - 属性 ...
- Java中synchronized
原文地址 synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种:1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用 ...