题意:

求多个串的最长公共子串

这里用的是O(n)的后缀自动机写法

我后缀数组的专题有nlog(n)写法的

题解:

对于其中的一个串建立后缀自动机

然后对于后缀自动机上面的每一个节点求出每一个节点最长可以匹配的子串(用maxx【】数组存下)

但是在后缀自动机上面有些节点没有走过,但是是某些走过的点的父亲节点因此也是有值的

for (int i = tot; i; i--)
maxx[fail[i]] = max(maxx[fail[i]], min(len[fail[i]], maxx[i])); 然后求一个所有最小值里面的最大值就好了
 #include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../in.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 1e6 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
char s[maxn]; struct Suffix_Automaton {
int last, tot, nxt[maxn << ][], fail[maxn << ];//last是未加入此字符前最长的前缀(整个串)所属的节点的编号
int len[maxn << ];// 最长子串的长度 (该节点子串数量 = len[x] - len[fail[x]])
int maxx[maxn << ]; void init() {
tot = last = ;
fail[] = len[] = ;
for (int i = ; i < ; i++) nxt[][i] = ;
} void extend(int c) {
int u = ++tot, v = last;
for (int i = ; i < ; i++) nxt[u][i] = ;
fail[u] = ;
len[u] = len[v] + ;
for (; v && !nxt[v][c]; v = fail[v]) nxt[v][c] = u;
if (!v) fail[u] = ;
else if (len[nxt[v][c]] == len[v] + ) fail[u] = nxt[v][c];
else {
int now = ++tot, cur = nxt[v][c];
len[now] = len[v] + ;
memcpy(nxt[now], nxt[cur], sizeof(nxt[cur]));
fail[now] = fail[cur];
fail[cur] = fail[u] = now;
for (; v && nxt[v][c] == cur; v = fail[v]) nxt[v][c] = now;
}
last = u;
//return len[last] - len[fail[last]];//添加一个新的字符产生的新的本质不同的子串数目
} int minn[maxn << ]; void match() {
mem(maxx, );
int n = strlen(s);
int p = , maxlen = ;
for (int i = ; i < n; i++) {
int c = s[i] - 'a';
if (nxt[p][c]) p = nxt[p][c], maxlen++;
else {
for (; p && !nxt[p][c]; p = fail[p]);
if (!p) p = , maxlen = ;
else maxlen = len[p] + , p = nxt[p][c];
}
maxx[p] = max(maxx[p], maxlen);
}
for (int i = tot; i; i--)
maxx[fail[i]] = max(maxx[fail[i]], min(len[fail[i]], maxx[i]));
for (int i = tot; i; i--)
if (minn[i] == - || minn[i] > maxx[i]) minn[i] = maxx[i];
} } sam; int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
sfs(s);
int n = strlen(s);
sam.init();
for (int i = ; i < n; ++i) sam.extend((s[i] - 'a'));
for (int i = ; i <= sam.tot; i++) sam.minn[i] = -;
while (~sfs(s)) sam.match();
int ans = ;
for (int i = ; i <= sam.tot; i++) ans = max(ans, sam.minn[i]);
printf("%d\n", ans);
#ifndef ONLINE_JUDGE
cout << "Totle Time : " << (double) clock() / CLOCKS_PER_SEC << "s" << endl;
#endif
return ;
}

多个串的最长公共子串 SPOJ - LCS2 后缀自动机的更多相关文章

  1. [codevs3160]最长公共子串解题报告|后缀自动机

    给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 样例就觉得不能更眼熟啊...好像之前用后缀数组做过一次 然后发现后缀自动机真的好好写啊...(当然当时学后缀数组的时候也这么认为... 这 ...

  2. SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)

    http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...

  3. 【poj1226-出现或反转后出现在每个串的最长公共子串】后缀数组

    题意:求n个串的最长公共子串,子串出现在一个串中可以是它的反转串出现.总长<=10^4. 题解: 对于每个串,把反转串也连进去.二分长度,分组,判断每个组. #include<cstdio ...

  4. SPOJ LCS2 多个串的最长公共子串

    这里串最多有10个,找所有串的最长公共子串 这里后缀自动机做,以第一个串建立后缀自动机,后面的串一个个去匹配,每次得到当前串在可到达状态上所能得到的最长后缀长度 拿所有串匹配后得到的结果进行计算 #i ...

  5. BZOJ 4032 Luogu P4112 [HEOI2015]最短不公共子串 (DP、后缀自动机)

    这其实是道水题... 题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4032 (luogu)https://www.luog ...

  6. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  7. [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串

    题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...

  8. luogu 2463 [SDOI2008]Sandy的卡片 kmp || 后缀数组 n个串的最长公共子串

    题目链接 Description 给出\(n\)个序列.找出这\(n\)个序列的最长相同子串. 在这里,相同定义为:两个子串长度相同且一个串的全部元素加上一个数就会变成另一个串. 思路 参考:hzwe ...

  9. SAM求多个串的最长公共子串

    又学到一个\(SAM\)的新套路QvQ 思路 考虑用其中的一个串建个\(SAM\),然后用其他的串在上面匹配,匹配时更新答案 首先有一个全局变量\(len\),表示当前已匹配的长度.假设目前在点\(u ...

随机推荐

  1. JS检查断网

    window.addEventListener('load', function() { function updateOnlineStatus(event) { var condition = na ...

  2. MySQL数据库学习初步

    我使用的环境是Win7,开始学习PHP和MySQL,并且买了本<Head First PHP & MySQL>,可以从Head First Labs官网获得HeadFirst系列书 ...

  3. jmeter 不同线程组之间传递变量2

    方法1  通过变量传递参数: 第一个脚本: HTTP Request_新建出差申请单_登录,关联出参数token.companyId.userId.userName 1.添加后置处理器:BeanShe ...

  4. Anjular的ng-repeat

    Anjular的ng-repeat不会循环一个二维集合中的一维集合.举个例子:集合 list=  {1,2,{0,1,2},23,222},small={0,1,2},使用ng-repeat" ...

  5. 递归,装饰器,python常用内置方法

    **递归**        def calc(n):            print(n)            if int(n / 2) == 0:  条件判断                r ...

  6. python 正则匹配

    正则表达式模式 模式字符串使用特殊的语法来表示一个正则表达式: 字母和数字表示他们自身.一个正则表达式模式中的字母和数字匹配同样的字符串. 多数字母和数字前加一个反斜杠时会拥有不同的含义. 标点符号只 ...

  7. vue 项目 跳转 页面 不刷新 问题

    vue项目中需要导出下载客户数据,因为数据太多,响应太慢.后台直接上传给七牛  然后返回一个下载链接  前端通过跳转链接 来下载 riskManagementApi.friendExprotAll(t ...

  8. Vue学习笔记【9】——Vue指令之v-for和key属性

    迭代数组(普通数组.对象数组) <ul> <li v-for="(item, i) in list">索引:{{i}} --- 姓名:{{item.name ...

  9. v-on 绑定单个或多个事件

    <div id="app07"> <!-- v-on:click 鼠标点击事件--> <a v-on:click="DoSomething& ...

  10. go new

    go new 尽管没有构造函数,go有一个内置的函数new,可以用来分配一个类型需要的内存.new(X)和&X{}是等效的: goku := new(Saiyan) // 等效 goku := ...