http://www.lydsy.com/JudgeOnline/problem.php?id=2946

题意:给n个串,求最大公共子串。(1<=n<=5,每个串长度<=2000)

#include <bits/stdc++.h>
using namespace std;
const int N=2005<<1; struct sam {
int cnt, root, last, l[N], c[N][26], f[N], p[N], mx[N], mxl[N];
sam() { cnt=0; root=last=++cnt; }
void add(int x) {
int now=last, a=++cnt; last=a;
l[a]=l[now]+1;
for(; now && !c[now][x]; now=f[now]) c[now][x]=a;
if(!now) { f[a]=root; return; }
int q=c[now][x];
if(l[q]==l[now]+1) { f[a]=q; return; }
int b=++cnt;
memcpy(c[b], c[q], sizeof c[q]);
l[b]=l[now]+1;
f[b]=f[q];
f[q]=f[a]=b;
for(; now && c[now][x]==q; now=f[now]) c[now][x]=b;
}
void build(char *s) {
int len=strlen(s);
for(int i=0; i<len; ++i) add(s[i]-'a');
for(int i=1; i<=cnt; ++i) mx[l[i]]++;
for(int i=1; i<=len; ++i) mx[i]+=mx[i-1];
for(int i=1; i<=cnt; ++i) p[mx[l[i]]--]=i;
for(int i=1; i<=cnt; ++i) mx[i]=l[i];
}
void find(char *s) {
int x, now=root, t=0, len=strlen(s);
for(int i=0; i<len; ++i) {
x=s[i]-'a';
if(c[now][x]) ++t, now=c[now][x];
else {
while(now && !c[now][x]) now=f[now];
if(!now) t=0, now=root;
else t=l[now]+1, now=c[now][x];
}
mxl[now]=max(mxl[now], t);
}
for(int i=cnt; i; --i) {
x=p[i];
mx[x]=min(mx[x], mxl[x]);
if(mxl[x] && f[x]) mxl[f[x]]=l[f[x]];
mxl[x]=0;
}
}
int getans() {
int ret=0;
for(int i=1; i<=cnt; ++i) ret=max(ret, mx[i]);
return ret;
}
}solver; char s[N];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
solver.build(s); --n;
while(n--) scanf("%s", s), solver.find(s);
printf("%d\n", solver.getans());
return 0;
}

  


复习了下sam....

【BZOJ】2946: [Poi2000]公共串的更多相关文章

  1. BZOJ 2946: [Poi2000]公共串

    2946: [Poi2000]公共串 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 787  Solved: 342[Submit][Status][D ...

  2. BZOJ 2946: [Poi2000]公共串( 后缀自动机 )

    一个串建后缀自动机, 其他串在上面跑, 然后用当前串跑的去更新全部 ------------------------------------------------------------------ ...

  3. bzoj 2946 [Poi2000]公共串——后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2946 对每个串都建一个后缀自动机,然后 dfs 其中一个自动机,记录同步的话在别的自动机上走 ...

  4. BZOJ 2946 [Poi2000]公共串 (二分+Hash/二分+后缀数组/后缀自动机)

    求多串的最长公共字串. 法1: 二分长度+hash 传送门 法2: 二分+后缀数组 传送门 法3: 后缀自动机 拿第一个串建自动机,然后用其他串在上面匹配.每次求出SAM上每个节点的最长匹配长度后,再 ...

  5. BZOJ 2946 POI2000 公共串 后缀自动机(多串最长公共子串)

    题意概述:给出N个字符串,每个串的长度<=2000(雾...可能是当年的年代太久远机子太差了),问这N个字符串的最长公共子串长度为多少.(N<=5) 抛开数据结构,先想想朴素做法. 设计一 ...

  6. BZOJ 2946 [Poi2000]公共串 ——后缀自动机

    任意选择一个串作为模式串,构建出后缀自动机. 然后用其他的串在后缀自动机上跑匹配. 然后就到了理解后缀自动机性质的时候. 在某一个节点的最大值是可以沿着parent树上传的. 然后用dp[i][j]表 ...

  7. bzoj 2946: [Poi2000]公共串【SAM】

    对第一个串建SAM,把剩下的串在上面跑,每次跑一个串的时候在SAM的端点上记录匹配到这的最大长度,然后对这些串跑的结果取min,然后从这些节点的min中取max就是答案 注意在一个点更新后它的祖先也会 ...

  8. 【BZOJ 2946】 2946: [Poi2000]公共串 (SAM)

    2946: [Poi2000]公共串 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1063  Solved: 469 Description      ...

  9. BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案

    BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description          给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l        读入单 ...

随机推荐

  1. angular 强制刷新路由,重新加载路由

    angular js ui-route 在使用时默认不是不会刷新路由的,所有有些时候我们需要主动刷新路由. 主动刷新方法是: <a ui-sref="profitManage" ...

  2. BZOJ 1004

    一道奇怪的数学题.为了这道题我看了很多题解,到底还是一知半解..整个感觉就是上了一场数学课. HNOI2008 Cards 题目描述 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有 ...

  3. grep与egrep

    当只有一个匹配条件时:egrep pattern file等价于grep -E pattern file 例如: 当多个匹配条件时,只能用egrep -e pattern1 -e pattern2 - ...

  4. /lib /usr/lib /usr/local/lib区别

    昨天问我/usr/lib 和/usr/local/lib 我仅记得一个是系统的,一个是用户的,于是今天查了查,有两篇文章介绍的不错,usr 很多人都认为是user缩写,其实不然,是unix syste ...

  5. 《ASP.NET1200例》未能找到元数据文件解决办法

         今天在做一个项目的时候,遇到这样的一个问题,我用的是三层结构的.未能找到元数据文件“D:\SYSTEM\桌面\MyExam\MyExam\MyExamBLL\bin\Debug\BLL.dl ...

  6. Timer1控件的属性

  7. 【leetcode】Excel Sheet Column Number

    Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appea ...

  8. Java for LeetCode 162 Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  9. 【JAVA、C++】LeetCode 009 Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 解题思路一: 双指针法,逐位判断 Java代码如下 ...

  10. jquery去掉或者替换字符,设置指定options为selected状态

    <html> <body> <div><select id="queryYear">                 <opt ...