【刷题】SPOJ 1812 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
Solution
提出一个串做SAM
做完后,将剩下的所有串与SAM进行匹配
每次匹配时记录每个节点以它为结尾最长能够匹配多长,然后将这次匹配的结果与保存的最后的答案取min(我们要保证剩下的所有的串与第一个串匹配的是同一段)
全部匹配完后在每个节点取max就是答案了
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=10+5,MAXS=100000+10,inf=0x3f3f3f3f;
int n,las=1,tot=1,len[MAXS<<1],fa[MAXS<<1],ch[MAXS<<1][30],ans,st[MAXS<<1],rk[MAXS<<1],cnt[MAXS],now[MAXS<<1];
char s[MAXN][MAXS];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void extend(int c)
{
int p=las,np=++tot;
las=np;
len[np]=len[p]+1;
while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];
if(!p)fa[np]=1;
else
{
int q=ch[p][c];
if(len[q]==len[p]+1)fa[np]=q;
else
{
int nq=++tot;
fa[nq]=fa[q];
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
len[nq]=len[p]+1,fa[np]=fa[q]=nq;
while(p&&ch[p][c]==q)ch[p][c]=nq,p=fa[p];
}
}
}
inline int match(int w)
{
memset(now,0,sizeof(now));
for(register int i=1,j=1,res=0,lt=strlen(s[w]+1);i<=lt;++i)
{
int c=s[w][i]-'a'+1;
if(ch[j][c])res++,j=ch[j][c];
else
{
while(j&&!ch[j][c])j=fa[j];
if(!j)res=0,j=1;
else res=len[j]+1,j=ch[j][c];
}
chkmax(now[j],res);
}
for(register int i=tot;i>=1;--i)chkmax(now[fa[rk[i]]],now[rk[i]]);
for(register int i=1;i<=tot;++i)chkmin(st[i],now[i]);
}
int main()
{
while(scanf("%s",s[++n]+1)!=EOF);
n--;
for(register int i=1,lt=strlen(s[1]+1);i<=lt;++i)extend(s[1][i]-'a'+1);
for(register int i=1;i<=tot;++i)st[i]=len[i];
for(register int i=1;i<=tot;++i)cnt[len[i]]++;
for(register int i=1,lt=strlen(s[1]+1);i<=lt;++i)cnt[i]+=cnt[i-1];
for(register int i=1;i<=tot;++i)rk[cnt[len[i]]--]=i;
for(register int i=2;i<=n;++i)match(i);
for(register int i=2;i<=tot;++i)chkmax(ans,st[i]);
write(ans,'\n');
return 0;
}
【刷题】SPOJ 1812 LCS2 - Longest Common Substring II的更多相关文章
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- SPOJ 1812 LCS2 - Longest Common Substring II
思路 后缀自动机求多串的最长公共子串 对第一个建出后缀自动机,其他的在SAM上匹配,更新到一个节点的匹配长度最大值即可,最后对所有最大值取min得到一个节点的答案,对所有节点答案求max即可 然后注意 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- SPOJ:LCS2 - Longest Common Substring II
题面 给定一些字符串,求出它们的最长公共子串 输入格式 输入至多 \(10\) 行,每行包含不超过 \(100000\)个的小写字母,表示一个字符串 输出格式 一个数,最长公共子串的长度 若不存在最长 ...
- spoj1812 LCS2 - Longest Common Substring II
地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags A string is fi ...
- 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 ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
随机推荐
- 接口测试工具postman(五)批量执行测试用例
1.准备好测试用例及相关数据 2.点击Run按钮 3.选择运行collection或者folder 4.运行完成
- 韦大仙python--购物车
程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 ...
- 在deepin系统中制作桌面快捷方式
在使用deepin-wine 安装一些软件的时候,每次启动都需要到.deepinwine目录下运行deepin-wine xx.exe.笔者在安装过HeidiSql之后,一直苦于这种情况.比较好的解决 ...
- LabVIEW初篇---前言
最早接触labview,是研二的时候,2007年,当时为了补贴家用,改善生活.自己拿着本科毕业证去找工作,去一个企业面试,当时,面试的主考官,问了会什么吗,比如PLC.单片机啥的?那时候的自己,基本上 ...
- Vue 编程之路(三)—— Vue 中子组件在父组件的 v-for 循环里,父组件如何调取子组件的事件
(标题的解决方案在第二部分) 最近公司的一个项目中使用 Vue 2.0 + element UI 实现一个后台管理系统的前端部分,属于商城类型. 一.前期思路: 其中在“所有订单”页面,UI 给的设计 ...
- python程序设计——面向对象程序设计:类
理解面向对象 基本原则是,计算机程序由多个能够起到子程序作用的单元或对象组合而成 关键性观念是,数据以及对数据的操作封装在一起,组成一个相互依存.不可分割的整体,即对象 python面向对象特性 完全 ...
- hibernate提示Unknown entity: :xxx
错误提示: org.hibernate.MappingException: Unknown entity: org.dao.po.Role at org.hibernate.internal.Sess ...
- windows远程连接失败问题排查思路
一般情况下,对WIN7的远程连接只需要5步即可完成远程连接的设置: 1).用户是否设置了密码 2).计算机属性-允许远程登录 3).设置计算机永不睡眠 4).关闭防火墙或者设置入站规则 5).排查Re ...
- Centos7 下nginx nginx-1.13.4 安装
环境:CentOS Linux release 7.3.1611 (Core) Linux localhost.localdomain 3.10.0-514.26.2.el7.x86_64 #1 S ...
- Alphabetic Removals(模拟水题)
You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly ...