SPOJ Longest Common Substring II
题目连接:戳我
题目大意:求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的更多相关文章
- 后缀自动机(SAM):SPOJ Longest Common Substring II
Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...
- 2018.12.15 spoj Longest Common Substring II(后缀自动机)
传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- 【SPOJ】Longest Common Substring II
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 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 ...
- Longest Common Substring II SPOJ - LCS2 (后缀自动机)
Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...
- spoj1812 LCS2 - Longest Common Substring II
地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags A string is fi ...
随机推荐
- php Pthread 多线程 (三) Mutex 互斥量
当我们用多线程操作同一个资源时,在同一时间内只能有一个线程能够对资源进行操作,这时就需要用到互斥量了.比如我们对同一个文件进行读写操作时. <?php class Add extends Thr ...
- Spring框架之演示JDBC的模板类
1. 步骤一:创建数据库的表结构 create database spring_day03; use spring_day03; create table t_account( id int prim ...
- 合成冷色黑暗恐怖魔法师图片的PS教程
教程主要使用Photoshop合成黑暗风格的魔法师施法场景,整体的场景效果以冷色风格为主,加上素材的叠加完成最终效果图,希望朋友可以喜欢.效果图: 先把背景拖进去,用工具吧字母弄掉. 加一个调色图层 ...
- sdkbox 接facebook
详情参见:http://sdkbox-doc.github.io/en/plugins/facebook/v3-js/#manual-integration 一步不落然后 *** Terminatin ...
- tty linux 打开和设置范例
http://bbs.csdn.net/topics/340184140 /************************************************************** ...
- java利用递归实现汉诺塔算法
package 汉诺塔; //引入Scanner包,用于用户输入 import java.util.Scanner; public class 汉诺塔算法 { public static void m ...
- 2018.10.15 bzoj3564: [SHOI2014]信号增幅仪(坐标处理+最小圆覆盖)
传送门 省选考最小圆覆盖? 亦可赛艇(你们什么都没看见) 在大佬的引领下成功做了出来. 就是旋转坐标使椭圆的横轴跟xxx轴平行. 然后压缩横坐标使得其变成一个圆. 然后跑最小覆盖圆就可以了. 注意题目 ...
- python面向对象-1方法、构造函数
类是指:描述一种事物的定义,是个抽象的概念 实例指:该种事物的一个具体的个体,是具体的东西 打个比方: “人”是一个类.“张三”是人类的一个具体例子 在编程时也是同样的道理,你先自己定义一个“类”,当 ...
- 深入浅析JavaScript中with语句的理解
JavaScript 有个 with 关键字, with 语句的原本用意是为逐级的对象访问提供命名空间式的速写方式. 也就是在指定的代码区域, 直接通过节点名称调用对象. with语句的作用是暂时改变 ...
- Spring3.x错误--Pointcut is not well-formed:expecting 'name pattern' at...
Spring3.x错误: 解决方法: (*com.dayang.service..*(..)) *和com.dayang.之间有空格