SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS
LCS2 - Longest Common Substring II
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.
Example
Input:
alsdfkjfjkdsal
fdjskalajfkdsla
aaaajfaaaa Output:
2
Notice: new testcases added
题意:
求多个串的最长公共字串
题解:
将第一个串建立后缀自动机
根据拓扑排序更新每个状态节点下所能向前延伸的长度,每次匹配一个串都更新即可
是不是很模糊
#include <bits/stdc++.h>
inline long long read(){long long x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;}
using namespace std; const int N = 3e5+; const long long mod = ; int isPlus[N * ],endpos[N * ];int d[N * ];
int tot,slink[*N],trans[*N][],minlen[*N],maxlen[*N],pre;
int newstate(int _maxlen,int _minlen,int* _trans,int _slink){
maxlen[++tot]=_maxlen;minlen[tot]=_minlen;
slink[tot]=_slink;
if(_trans)for(int i=;i<;i++)trans[tot][i]=_trans[i],d[_trans[i]]+=;
return tot;
}
int add_char(char ch,int u){
int c=ch-'a',v=u;
int z=newstate(maxlen[u]+,-,NULL,);
isPlus[z] = ;
while(v&&!trans[v][c]){trans[v][c]=z;d[z]+=;v=slink[v];}
if(!v){ minlen[z]=;slink[z]=;return z;}
int x=trans[v][c];
if(maxlen[v]+==maxlen[x]){slink[z]=x;minlen[z]=maxlen[x]+;return z;}
int y=newstate(maxlen[v]+,-,trans[x],slink[x]);
slink[z]=slink[x]=y;minlen[x]=minlen[z]=maxlen[y]+;
while(v&&trans[v][c]==x){trans[v][c]=y;d[x]--,d[y]++;v=slink[v];}
minlen[y]=maxlen[slink[y]]+;
return z;
}
void init_sam() {
for(int i = ; i <= tot; ++i)
for(int j = ; j < ; ++j) trans[i][j] = ;
pre = tot = ;
} int dp[N],ans,n,vis[N],cnt[N],pos[N];
char a[N];
int main() {
int f = ;
while(scanf("%s",a)!=EOF) {
n = strlen(a);
if(f) {
init_sam();
for(int i = ; i < n; ++i)
pre = add_char(a[i],pre);
f = ;
for(int i = ; i <= n; ++i)cnt[i] = ;
for(int i = ; i <= tot; ++i)cnt[maxlen[i]] += ;
for(int i = ; i <= n; ++i)cnt[i]+=cnt[i-];
for(int i = tot; i >= ; --i)pos[cnt[maxlen[i]]--] = i;
}
else
{
int now = ,sum = ;
for(int i = ; i < n; ++i)
{
if(trans[now][a[i]-'a']) {
sum++;
now = trans[now][a[i]-'a'];
dp[now] = max(dp[now],sum);
}
else {
while(now) {
now = slink[now];
if(trans[now][a[i]-'a']) break;
}
if(!now) now = ,sum = ;
else
{
sum = maxlen[now] + ;
now = trans[now][a[i]-'a'];
dp[now] = max(dp[now],sum);
}
}
}
for(int i = tot; i >= ; --i) {
int v = pos[i];
maxlen[v] = min(maxlen[v],dp[v]);
dp[slink[v]] = max(dp[slink[v]],dp[v]);
dp[v] = ;
}
}
}
int ans = ;
for(int i = ; i <= tot; ++i) {
ans = max(ans,maxlen[i]);
}
printf("%d\n",ans);
return ;
}
/*
aabbcd
babbefg
cdbbabb
*/
SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS的更多相关文章
- SPOJ LCS2 Longest Common Substring II ——后缀自动机
后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- 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(后缀自动机)题解
题意: 求\(n\)个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,然后枚举所有串.对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少), ...
- SPOJ LCS2 - Longest Common Substring II 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...
- Virtual Judge SPOJ - LCS2 Longest Common Substring II
https://vjudge.net/problem/SPOJ-LCS2 SPOJ注册看不到验证码,气到暴毙,用vjudge写的. 注意!(对拍的时候发现)这份代码没有对只有一个字符串的情况进行处理! ...
随机推荐
- MapReduce1 工作机制
本文转自:Hadoop MapReduce 工作机制 工作流程 作业配置 作业提交 作业初始化 作业分配 作业执行 进度和状态更新 作业完成 错误处理 作业调度 shule(mapreduce核心)和 ...
- Write a function that generates one of 3 numbers according to given probabilities
You are given a function rand(a, b) which generates equiprobable random numbers between [a, b] inclu ...
- Topcoder SRM 664 DIV 1
BearPlays 快速幂 题意: 给你两个数A,B,有种操作是将大的数减去小的数,并将小的数乘以2.反复k次,问你最后的小的数回是多少. 题解: 由于整个过程$A+B$的值是不会改变的.现在令$S= ...
- 基于Java实现的选择排序算法
选择排序和冒泡排序同样是基础排序算法,现在也做个学习积累. 简述 选择排序算法较为稳定,基本上都是O(n2)的时间复杂度,规模越小排序越快,不需要占用额外空间.其实选择排序原理很简单,就是在未排序序列 ...
- 【转】java:多网卡环境下获取MAC地址
http://blog.csdn.net/10km/article/details/78569962 JDK6以后 java.net.NetworkInterface提供了完整的方法用于获取网络设备信 ...
- nohup 输出重定向
今天在使用nohup命令的时候,每次后台执行生成的日志文件名都为nohup.out,现需要改变nohup命令生成的文件名. 在shell中,文件描述符通常是:STDIN标准输入,STDOUT标准输出, ...
- APT攻击:91%的攻击是利用电子邮件
一封假冒的"二代医疗保险补充保险费扣费说明",导致上万家中小型企业的资料被窃;一封伪装银行交易纪录的钓鱼信件,让韩国爆发史上最大黑客攻击. APT攻击通常会以电子邮件的形式出现,邮 ...
- struts2学习笔记之表单标签的详解:s:checkbox/radio/select/optiontransferselect/doubleselect/combobox
struts2中的表单标签都是以s标签的方式定义的,同时,struts2为所有标签都提供了一个模板,C:\Users\180172\Desktop\struts2-core-2.2.1.1.jar\t ...
- 【Excle数据透视表】如何隐藏数据透视表中行字段的”+/-”按钮
如下图:新建的数据透视表中有存在"+/-"符号,导致数据透视图不太美观,那么怎么处理呢? 解决方案 单击"显示"组中的"+/-"按钮显示或隐 ...
- 自定义序列化技术3 (.net 序列化技术) C++ 调用C# DLL
打开SerializableAttribute利用里面的函数进行编辑. // sparse.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" ...