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. 【转】Solr5.3.1定时增量添加索引和重做索引

    本文转自:https://code.google.com/p/solr-dataimport-scheduler/ Solr Data Import Hander Scheduler 说明:Solr官 ...

  2. 如何用adb logcat保存日志

    //将log 保存到当前目录下 adb logcat -v time >a.log //log过滤 adb logcat | grep MyAppName //清除log adb logcat ...

  3. Android 和iOS中 View的滚动

    在最近的程序中用到了Android中的View的滚动,记录一下,待总结.

  4. iOS 中NSOperationQueue,Grand Central Dispatch , Thread的上下关系和区别

    In OS X v10.6 and later, operation queues use the libdispatch library (also known as Grand Central D ...

  5. ini 文件操作记要(1): 使用 TIniFile

    ini 文件操作记要(1): 使用 TIniFile unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Class ...

  6. POJ 3977

    Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 1373   Accepted: 228 Descriptio ...

  7. DP:Space Elevator(POJ 2392)

    太空电梯 题目大意:一群牛想造电梯到太空,电梯都是由一个一个块组成的,每一种块不能超过这个类型的高度,且每一种块都有各自的高度,有固定数量,问最高能造多高. 这题就是1742的翻版,对ai排个序就可以 ...

  8. DFS:Red and Black(POJ 1979)

    红与黑 题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间? 不多说,DFS深搜即可,水题 注意一下不要把行和列搞错就好了,我就是那样弄错过一 ...

  9. 20145221 《Java程序设计》实验报告二:Java面向对象程序设计

    20145221 <Java程序设计>实验报告二:Java面向对象程序设计 实验要求 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O. ...

  10. 希尔排序( Shell Sort)

    原文地址:http://www.stoimen.com/blog/,在此感谢作者! Insertion sort is a great algorithm, because it’s very int ...