spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】
多串LCS很适合SA但是我要学SAM
对第一个串求SAM,然后把剩下的串在SAM上跑,也就是维护p和len,到一个点,如果有ch[p][c],就p=ch[p][c],len++,否则向fa找最下的有c[p][c]的p,然后len=dis[p]+1,p=ch[p][c],否则就p=root,len=0(这个len每到一个节点就更新这个节点的f)
然后注意到在parent树上,因为每个节点代表的right集合是儿子的并集,所以f[u]是可以更新f[fa[u]]的,所以从底向上更新一遍(注意先更新!!)
最终点u的f就是每个串的f与dis[u]取min,然后ans在这些点上取max
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=500005;
int n,m=1,fa[N],ch[N][27],dis[N],cur=1,con=1,la,f[N][15],c[N],a[N],ans;
char s[N];
void ins(int c,int id)
{
la=cur,dis[cur=++con]=id;
int p=la;
for(;p&&!ch[p][c];p=fa[p])
ch[p][c]=cur;
if(!p)
fa[cur]=1;
else
{
int q=ch[p][c];
if(dis[q]==dis[p]+1)
fa[cur]=q;
else
{
int nq=++con;
dis[nq]=dis[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];
fa[q]=fa[cur]=nq;
for(;ch[p][c]==q;p=fa[p])
ch[p][c]=nq;
}
}
}
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++)
ins(s[i]-'a',i);
for(int i=1;i<=con;i++)
f[i][1]=dis[i];
while(~scanf("%s",s+1))
{
n=strlen(s+1),m++;
for(int i=1,p=1,len=0;i<=n;i++)
{
int c=s[i]-'a';
if(ch[p][c])
len++,p=ch[p][c],f[p][m]=max(f[p][m],len);
else
{
for(;p&&!ch[p][c];p=fa[p]);
if(!p)
p=1,len=0;
else
len=dis[p]+1,p=ch[p][c],f[p][m]=max(f[p][m],len);
}
}
}
for(int i=1;i<=con;i++)
c[dis[i]]++;
for(int i=1;i<=con;i++)
c[i]+=c[i-1];
for(int i=1;i<=con;i++)
a[c[dis[i]]--]=i;
for(int j=2;j<=m;j++)
for(int i=con;i>=1;i--)
f[fa[a[i]]][j]=max(f[fa[a[i]]][j],f[a[i]][j]);
for(int i=1;i<=con;i++)
{
for(int j=2;j<=m;j++)
f[i][1]=min(f[i][1],f[i][j]);
ans=max(ans,f[i][1]);
}
printf("%d\n",ans);
return 0;
}
spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】的更多相关文章
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 【SPOJ - LCS2】Longest Common Substring II【SAM】
题意 求出多个串的最长公共子串. 分析 刚学SAM想做这个题的话最好先去做一下那道codevs3160.求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长 ...
- spoj SUBLEX - Lexicographical Substring Search【SAM】
先求出SAM,然后考虑定义,点u是一个right集合,代表了长为dis[son]+1~dis[u]的串,然后根据有向边转移是添加一个字符,所以可以根据这个预处理出si[u],表示串u后加字符能有几个本 ...
- Java for LeetCode 030 Substring with Concatenation of All Words【HARD】
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- HDU-1423-Greatest Common Increasing Subsequence-最长公共上升子序列【模版】
This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence ...
- 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- spoj NSUBSTR - Substrings【SAM】
先求个SAM,然后再每个后缀的对应点上标记si[nw]=1,造好SAM之后用吧parent树建出来把si传上去,然后用si[u]更新f[max(u)],最后用j>i的[j]更新f[i] 因为每个 ...
- 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 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
随机推荐
- SpringBoot学习之@SpringBootApplication注解
下面是我们经常见到SpringBoot启动类代码: @SpringBootApplicationpublic class DemoApplication extends SpringBootServl ...
- mysql使用“.frm”文件恢复表结构
mysql创建每张表后都会在“mysql安装目录/data/数据库名/”目录下创建一个“表名.frm”文件. 该.frm文件并不能直接打开,但是它可以帮助你恢复你的表结构~~ 具体操作如下: 我现在准 ...
- Please verify that your device’s clock is properly set, and that your signing certificate is not exp
真机调试的时候出现此类警告,之前也遇到过,但是一直没总结,今天总结一下 出现这样的问题大概有几个解决方法: 1.最简单的一种----假设你的证书是近期才申请的没什么问题.或者说前几天測试还没问题,突然 ...
- c# 连接各种数据库 Access、Server等
1.C#连接连接Access程序代码: using System.Data;using System.Data.OleDb;..string strConnection="Provider= ...
- BZOJ 1042: [HAOI2008]硬币购物 容斥+背包
1042: [HAOI2008]硬币购物 Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请 ...
- 自己写的canvas 手写画板
<!DOCTYPE html> <html> <head> <title>canvas</title> <meta charset=& ...
- 微信小程序 navigateTo 传对象参数
当微信小程序navigateTo传入参数是个object时,请使用JSON.strtingify将object转化为字符串,代码如下: wx.navigateTo({ url: '../sendChe ...
- Android 源码架构
我们都知道Android系统是一个开源工程,在网上可以下载到源代码. 一般在网上搜索一下,就会找到各种下载源代码的方法,比如使用Git和Repo,android源代码下载的网址是http://andr ...
- emacs环境配置
Cscope: 首先官网上下载cscope的源码包,解压进入,按照INSTALL的说明: ./configure make make install 但是在make时报如下错误:fatal error ...
- SPOJ:Collecting Candies(不错的DP)
Jonathan Irvin Gunawan is a very handsome living person. You have to admit it to live in this world. ...