SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)
A string is finite sequence of characters over a non-empty finite set Σ.
In this problem, Σ is the set of lowercase letters.
Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.
Now your task is a bit harder, for some given strings, find the length of the longest common substring of them.
Here common substring means a substring of two or more strings.
Input
The input contains at most 10 lines, each line consists of no more than 100000 lowercase letters, representing a string.
Output
The length of the longest common substring. If such string doesn't exist, print "0" instead.
题目大意:求多个字符串的最长公共子串。
思路:据说把多个串拼起来会MLE还是TLE?SPOJ太慢了……
给第一个串建立后缀自动机,然后把每个点的ans置为当前后缀的LCS,dp为当前某个串能匹配的最长后缀
然后每次弄完用每个点的dp更新ans……图是一个DAG……
哎呀好难说看代码吧……
代码(1500MS):
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = + ;
char buf[MAXN];
struct State {
State *fail, *go[];
int val, dp, ans;/*
State() :
fail(0), val(0) {
memset(go, 0, sizeof go);
}*/
}*root, *last;
State statePool[MAXN * ], *cur; void init() {
//memset(statePool, 0, 2 * strlen(buf) * sizeof(State));
cur = statePool;
root = last = cur++;
} void extend(int w) {
State *p = last, *np = cur++;
np->ans = np->val = p->val + ;
while (p && !p->go[w])
p->go[w] = np, p = p->fail;
if (!p) np->fail = root;
else {
State*q = p->go[w];
if (p->val + == q->val) np->fail = q;
else {
State *nq = cur++;
memcpy(nq->go, q->go, sizeof q->go);
nq->ans = nq->val = p->val + ;
nq->fail = q->fail;
q->fail = nq;
np->fail = nq;
while (p && p->go[w] == q)
p->go[w] = nq, p = p->fail;
}
}
last = np;
} inline void update_max(int &a, const int &b) {
if(a < b) a = b;
} inline void update_min(int &a, const int &b) {
if(a > b) a = b;
} struct Node {
State *p;
bool operator < (const Node &rhs) const {
return p->val < rhs.p->val;
}
} a[MAXN * ]; int main() {
init();
scanf("%s", buf);
for(char *pt = buf; *pt; ++pt)
extend(*pt - 'a');
int m = ;
for(State *p = statePool; p != cur; ++p) {
a[m++].p = p;
}
sort(a, a + m);
int n = ;
while(scanf("%s", buf) != EOF) {
++n;
State *t = root;
int l = ;
for(char *pt = buf; *pt; ++pt) {
int w = *pt - 'a';
if(t->go[w]) {
t = t->go[w];
update_max(t->dp, ++l);
}
else {
while(t && !t->go[w]) t = t->fail;
if(!t) l = , t = root;
else {
l = t->val;
t = t->go[w];
update_max(t->dp, ++l);
}
}
}
for(int i = m - ; i > ; --i) {
State *p = a[i].p;
update_min(p->ans, p->dp);
update_max(p->fail->dp, min(p->dp, max(p->fail->val, p->dp)));
p->dp = ;
}
}
int ans = ;
for(int i = m - ; i >= ; --i) update_max(ans, a[i].p->ans);
printf("%d\n", ans);
}
SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)的更多相关文章
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS
LCS2 - Longest Common Substring II no tags A string is finite sequence of characters over a non-emp ...
- SPOJ 1812 Longest Common Substring II
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1812 Longest Common Substring II(后缀自动机)
[题目链接] http://www.spoj.com/problems/LCS2/ [题目大意] 求n个串的最长公共子串 [题解] 对一个串建立后缀自动机,剩余的串在上面跑,保存匹配每个状态的最小值, ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- SPOJ LCS2 Longest Common Substring II ——后缀自动机
后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- SPOJ LCS Longest Common Substring(后缀自动机)题解
题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
随机推荐
- linux dentry cache 转自:http://blog.csdn.net/denzilxu/article/details/9188003
Linux dentry cache学习 每个dentry对象都属于下列几种状态之一: (1)未使用(unused)状态:该dentry对象的引用计数d_count的值为0,但其d_inode指针仍然 ...
- c语言描述的二分插入排序法
#include<stdio.h> #include<stdlib.h> //二分插入排序法 void BinsertSort(int a[],int n){ int low, ...
- mysql复制表数据,多表数据复制到一张表
对于mysql 复制表数据可以使用 insert into select 方式 示例: $sql="insert into icarzoo.provider(providerId,provi ...
- 与JSON相关的问题
1.JSON.stringify 与 JSON.parse 相关的问题 JSON.stringify 把字符串转化为字符串,JSON.parse把字符串转化为JSON格式 会出现的问题Unexpect ...
- LeetCode 中级 - 有序链表转换二叉搜索树(109)
给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10 ...
- ABAP术语-Interface Parameter
Interface Parameter 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/26/1081800.html Parameter t ...
- ABAP术语-HTML
HTML 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/19/1073298.html Hypertext Markup Language( ...
- 微信小程序数据传递基本
1.从后台服务器获取数据,然后存在JS,通过数据绑定显示在页面 后台获取数据: getUser: function () { var that = this; //function 里面已经不是thi ...
- hibernate笔记1
1. 数据库中的表关系 一对一.一对多(多对一).多对一 2. 如何确立表中的表关系 一对多的关系如何实现:使用外键约束,一的方称为主表,多的方称为从表. 外键:从表中有一列,该列的取值除了nul ...
- thinkphp 下多图ajax上传图片
碰到一个项目,有一个比较繁琐的功能6个ajax上传,基本上每个上传逻辑多不一样,记录一下 thinkphp的view页面: id方便找到这个元素 name一定要加 [ ] <div class= ...