[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... (其实是不 ...
随机推荐
- java变参
java变参是通过数组来实现的 Object[] addAll(Object[] array1, Object... array2)和Object[] addAll(Object[] array1, ...
- Spark源码分析之五:Task调度(一)
在前四篇博文中,我们分析了Job提交运行总流程的第一阶段Stage划分与提交,它又被细化为三个分阶段: 1.Job的调度模型与运行反馈: 2.Stage划分: 3.Stage提交:对应TaskSet的 ...
- 2015年多校联合训练第一场OO’s Sequence(hdu5288)
题意:给定一个长度为n的序列,规定f(l,r)是对于l,r范围内的某个数字a[i],都不能找到一个相应的j使得a[i]%a[j]=0.那么l,r内有多少个i,f(l,r)就是几. 问全部f(l,r)的 ...
- python 基础 7.1 datetime 获得时间
一 datatime 的使用 object timedeta tzinfo time data dat ...
- TP框架---thinkphp基础知识
php框架 发瑞 一.真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困 ...
- 怎么用cookie解决选项卡问题刷新后怎么保持原来的选项?
什么是cookie? Cookies虽然一般都以英文名呈现,但是它还是有一个可爱的中文名“小甜饼”.Cookies是指服务器暂存放在你的电脑里的txt格式的文本文件资料,主要用于网络服务器辨别电脑使用 ...
- 引用变量的类型强转以及InstanceOf方法的使用
引用到的类: class Person{ String name; } class Student extends Person{ String sut_no; } class ClassMate e ...
- mysql的分库分表
1 什么是分库分表 这里讨论的情况是一台机器上对应一个数据库. 分库的对象是表,分表的对象是行.分库是说把属于同一个模块的相关性很高的表放在同一个数据库中.分表是说把同一个表的的行分成多个子表,把各个 ...
- Error524 源站处理超时 Error 524: A timeout occurred
https://su.baidu.com/helps/index.html#/4/5a61e4b5b34f697f13234a5b Error524 源站处理超时 更新时间:2018-01-19 20 ...
- Nested Classes
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html package priceton; /* * Copyright (c) ...