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)的更多相关文章

  1. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  2. 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 ...

  3. 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 ...

  4. SPOJ 1812 Longest Common Substring II(后缀自动机)

    [题目链接] http://www.spoj.com/problems/LCS2/ [题目大意] 求n个串的最长公共子串 [题解] 对一个串建立后缀自动机,剩余的串在上面跑,保存匹配每个状态的最小值, ...

  5. SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)

    手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...

  6. SPOJ LCS2 Longest Common Substring II ——后缀自动机

    后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...

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

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

  8. SPOJ LCS Longest Common Substring(后缀自动机)题解

    题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...

  9. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

随机推荐

  1. 课时57.HTML被废弃的标签(掌握)

    1.为什么HTML中有一部分标签会被废弃? 因为当前HTML中的标签只有一个作用,就是用来添加语义,而早期的HTML标签中有一部分标签是没有语义的 有一部分标签是用来修改样式的 所以这部分标签就被淘汰 ...

  2. Redis集群的安装和使用

  3. 利用pt-query-digest分析MySQL慢查询

    1.用法与参数说明 pt-query-digest [OPTIONS] [FILES] [DSN] --create-review-table ##当使用--review参数把分析结果输出到表中时,如 ...

  4. sqlServer2014安装说明(windows7 64位)

    SqlServer2014安装说明(windows7 64位) 地址:https://www.microsoft.com/zh-cn/download/details.aspx?id=42299 1, ...

  5. Hibernate知识点小结汇总

    Hibernate部分 1.为什么要使用Hibernate开发你的项目呢?Hibernate的开发流程是怎么样的? 为什么要使用 ①.对JDBC访问数据库的代码做了封装,大大简化了数据访问层繁琐的重复 ...

  6. npm ERR! code: 'EPERM' (权限问题 errro permit)

    PS C:\Users\user\Desktop\test\my-project> npm run iview --save npm ERR! missing script: iview npm ...

  7. IPC进程间通信---消息队列

    消息队列 消息队列:消息队列是一个存放在内核中的消息链表,每个消息队列由消息队列标识符标识.与管道不同的是消息队 列存放在内核中,只有在内核重启(即操作系统重启)或者显式地删除一个消息队列时,该消息队 ...

  8. tidb损坏tikv节点怎么恢复集群

    tikv节点宕机(机器再起不来),或者数据节点被rm -rf 掉了怎么办 正常情况下tikv节点down掉了.此时不要去执行store delete  store_id .数据一般可以正常访问,但是如 ...

  9. django+xadmin在线教育平台(十六)

    7-7 modelform 提交我要学习咨询1 对应表userask form会对字段先做验证,然后保存到数据库中. 可以看到我们的forms和我们的model中有很多内容是一样的.我们如何让代码重复 ...

  10. CVE-2017-11882复现-office命令执行

    0x01 前言 11月14日,微软按照惯例发布了11月的安全更新,随后不久,安全公司EMBEDI在官方博客上公开了其向微软提交的编号为CVE-2017-11882的Office远程代码执行漏洞: ht ...