题目连接:戳我

题目大意:求n个字符串的最长公共子串。

它的简化版——这里

当然我们可以用SA写qwq,也可以用广义SAM写qwq

这里介绍纯SAM的写法。。。就是对其中一个建立后缀自动机,然后剩下的N-1个往上面匹配。

设\(sum[i]\)表示到以节点i为根的子树中,最长能够匹配到的最长的子串的长度。

匹配和它的弱化版基本一样,就是注意每次在parent tree上要用拓扑序从下往上更新答案。

注意每个点最大的匹配不能超过它所在类的longest

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 1000000
using namespace std;
int T,k,last=1,tot=1;
int a[MAXN],c[MAXN],ans[MAXN],sum[MAXN];
char s[MAXN];
struct Node{int son[26],ff,len;}t[MAXN<<1];
inline void extend(int c)
{
int p=last,np=++tot;last=np;
t[np].len=t[p].len+1;
while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
if(!p)t[np].ff=1;
else
{
int q=t[p].son[c];
if(t[q].len==t[p].len+1)t[np].ff=q;
else
{
int nq=++tot;
t[nq]=t[q];
t[nq].len=t[p].len+1;
t[q].ff=t[np].ff=nq;
while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%s",s+1);
int len=strlen(s+1);
for(int i=1;i<=len;i++) extend(s[i]-'a');
for(int i=1;i<=tot;i++) c[t[i].len]++;
for(int i=1;i<=tot;i++) c[i]+=c[i-1];
for(int i=1;i<=tot;i++) a[c[t[i].len]--]=i;
for(int i=1;i<=tot;i++) ans[i]=t[i].len;
while(scanf("%s",s+1)!=EOF)
{
len=strlen(s+1);
memset(sum,0,sizeof(sum));
int cur_ans=0;
int now=1;
for(int i=1;i<=len;i++)
{
if(t[now].son[s[i]-'a']) cur_ans++,now=t[now].son[s[i]-'a'];
else
{
while(now&&!t[now].son[s[i]-'a']) now=t[now].ff;
if(!now) cur_ans=0,now=1;
else cur_ans=t[now].len+1,now=t[now].son[s[i]-'a'];
}
sum[now]=max(sum[now],cur_ans);
}
for(int i=tot;i>=1;i--)
{
int cur=a[i];
sum[t[cur].ff]=max(sum[t[cur].ff],min(t[t[cur].ff].len,sum[cur]));
}
for(int i=1;i<=tot;i++) ans[i]=min(ans[i],sum[i]);
}
int maxx=0;
for(int i=1;i<=tot;i++) maxx=max(maxx,ans[i]);
printf("%d\n",maxx);
return 0;
}

SPOJ Longest Common Substring II的更多相关文章

  1. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

  2. 2018.12.15 spoj Longest Common Substring II(后缀自动机)

    传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...

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

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

  4. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

  5. 【SPOJ】Longest Common Substring II

    [SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...

  6. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

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

  8. Longest Common Substring II SPOJ - LCS2 (后缀自动机)

    Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...

  9. spoj1812 LCS2 - Longest Common Substring II

    地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags  A string is fi ...

随机推荐

  1. css浮动的元素居中

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. PS大神的作品,每张都是科幻大片!

    相信大家在网上一定见过 各种PS的作品 但是要想成为“PS大神”, 不仅仅要会P图, 最关键的就是脑洞! 同样的马路破坏效果 在大神操作后变成了大片! 摩托车换成了骏马 这效果果然不一般! 这个绝对牛 ...

  3. dateframe取数据

    import numpy as npimport pandas as pd## x1=[1,2,3,4]# x2=[4,5,6,7]# x3=[7,8,9,10]# df=pd.DataFrame(# ...

  4. Mockplus是如何节省你的原型时间的?

    还在用老牌原型工具一点点绘制产品原型吗?还在为实现一个满意的交互而绞尽脑汁吗?还在为无法和用户高效沟通而发愁吗?朋友,现在是快速原型的时代了.时间不等人,当你精雕细琢完成产品启动页的时候,别人的原型已 ...

  5. python list和函数之间的复制和原地址修改问题

    def change(a): a.pop() #自带的方法都是原地址修改 a=[,,] change(a) print (a)#直接修改了3. def change(a): a=[,,,] #复制操作 ...

  6. 删除pdf文件所有超链接

    最近在读deep learning 书Bengio那本,在Github上面下载的,下载回来全都是超链接, 超级烦,比如点一下梯度下降法,就直接跳转到数后尾的index. 我看书还喜欢老点,所以要把他们 ...

  7. jQuery DataTables插件分页允许输入页码跳转

    背景说明 项目中使用jQuery DataTables插件来实现分页表格,但是默认的分页样式不能输入页码进行跳转,在页数非常多的时候使用很不方便,最主要的还是没有达到产品部门的设计要求,所以我需要寻找 ...

  8. SGU 169 Numbers (找规律)

    题意:中文题,直接忽略... 析:先说说我的思路,我一看这个题第一感觉就是要找规律,要是挨着算,猴年马月都跑不完,更何况时间限制是0.25s,怎么找规律呢,我算了一下前10位,分别是8,1,1,3,1 ...

  9. 20170906工作日记--volley源码的相关方法细节学习

    1. 在StringRequest类中的75行--new String();使用方法 /** * 工作线程将会调用这个方法 * @param response Response from the ne ...

  10. Android热补丁技术—dexposed原理简析(阿里Hao)

    本文由嵌入式企鹅圈原创团队成员.阿里资深工程师Hao分享. 上篇文章<Android无线开发的几种常用技术>我们介绍了几种android移动应用开发中的常用技术,其中的热补丁正在被越来越多 ...