【SPOJ】Longest Common Substring II
【SPOJ】Longest Common Substring II
多个字符串求最长公共子串
还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献
由于是所有串,要对每个点每个字符串跑完后去最小值才是每个点的最终贡献
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const LL maxn=200000;
LL nod,last,n,T;
LL len[maxn],fail[maxn],son[maxn][26],Ans[maxn],sum[maxn],c[maxn],p[maxn];
char s[maxn];
inline void Insert(LL c){
LL np=++nod,p=last;
len[np]=len[p]+1;
last=np;
while(p&&!son[p][c]){
son[p][c]=np,
p=fail[p];
}
if(!p)
fail[np]=1;
else{
LL q=son[p][c];
if(len[q]==len[p]+1)
fail[np]=q;
else{
LL nq=++nod;
len[nq]=len[p]+1;
fail[nq]=fail[q];
memcpy(son[nq],son[q],sizeof(son[q]));
fail[np]=fail[q]=nq;
while(p&&son[p][c]==q){
son[p][c]=nq,
p=fail[p];
}
}
}
}
int main(){
nod=last=1;
scanf("%s",s);
LL Len=strlen(s);
for(LL i=0;i<Len;++i)
Insert(s[i]-'a');
for(LL i=1;i<=nod;++i)
c[len[i]]++;
for(int i=1;i<=nod;++i)
c[i]+=c[i-1];
for(int i=1;i<=nod;++i)
p[c[len[i]]--]=i;
for(int i=1;i<=nod;++i)
Ans[i]=len[i];
while(scanf("%s",s)!=EOF){
memset(sum,0,sizeof(sum));
LL now=1,cnt=0;
Len=strlen(s);
for(LL i=0;i<Len;++i){
LL c=s[i]-'a';
if(son[now][c])
++cnt,
now=son[now][c];
else{
while(now&&!son[now][c])
now=fail[now];
if(!now)
cnt=0,
now=1;
else
cnt=len[now]+1,
now=son[now][c];
}
sum[now]=max(sum[now],cnt);
}
for(LL i=nod;i>=1;--i)
sum[fail[p[i]]]=max(sum[fail[p[i]]],sum[p[i]]);
for(int i=1;i<=nod;++i)
Ans[i]=min(Ans[i],sum[i]);
}
LL ans=0;
for(LL i=1;i<=nod;++i)
ans=max(ans,Ans[i]);
printf("%lld",ans);
return 0;
}/*
fjewiofejhiofjmwopejeugfzjkjnfoakweldnfmoierhguiewkjfkowejrfoiwejsfd
jwierhdwuiek,dedjfkz[pjeowrfhuqigrfwerljfiuekdfkcdfheosf
*/
【SPOJ】Longest Common Substring II的更多相关文章
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- 【SPOJ】Longest Common Substring
[SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...
- 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 ...
- SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1812 Longest Common Substring II
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ 1812 Longest Common Substring II(后缀自动机)
[题目链接] http://www.spoj.com/problems/LCS2/ [题目大意] 求n个串的最长公共子串 [题解] 对一个串建立后缀自动机,剩余的串在上面跑,保存匹配每个状态的最小值, ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
随机推荐
- NRF52832 Mesh SDK 调试记录
1.Mesh SDK模型,Node节点在重启之后,心跳不能正常保持,即无法在次启动心跳的解决办法: 原因:主要是因为相关模型没有从Flash里面读取所致,因此只需要回复保存配置即可. 关键代码如下: ...
- go的安装及环境变量设置
1,go安装 https://studygolang.com/dl 官网下载,找自己需要的版本,傻瓜式安装 2.go的环境变量设置 windows下面要设置root和path root代表go安装路径 ...
- werkzeug/routing.py-Map()源码解析
Map类主要用来存储所有的url规则和一些配置参数的.其中有一些配置的值只存储在Map实例里,因为这些值影响着所有的规则,还有一些其他的默认规则可以被重写. 通过之前分析的add_url_rule源码 ...
- 【转载】 Asp.Net MVC网站提交富文本HTML标签内容抛出异常
今天开发一个ASP.NET MVC网站时,有个页面使用到了FCKEditor富文本编辑器,通过Post方式提交内容时候抛出异常,仔细分析后得出应该是服务器阻止了带有HTML标签内容的提交操作,ASP. ...
- Modelsim问题集锦
前言 收集工程调试中遇到的modelsim问题. 问题 (1)仿真发现时钟信号和理论上的数据信号没有边沿对齐. 解决:一般是时钟精度不匹配的问题. 如果想要1ns的精度则代码中的精度需设置为: v语法 ...
- 剑指offer-链表相关
剑指offer面试常考手撸算法题-链表篇 1. 从头到尾打印链表 class Solution { public: // 可以先压栈,再出栈到vector // 时间/空间:O(n) vector&l ...
- Wechat alert
企业微信号登录--注册企业号或者企业微信 添加子部门 部门添加成员 创建应用 需要接收告警的人员关注企业号 企业号已经被部门成员关注 企业号有一个可以发送消息的应用,一个授权管理员,可以使用应用给成员 ...
- 昨天521表白失败,我想用Python分析一下...表白记录和聊天记录
昨天跟喜欢的妹子表白了. 失败了!.下面是表白的聊天记录: (跟妹子已经认识一段时间) 我:灭嘤嘤,我喜欢你. 妹子:你干嘛? 我:今天520,跟你表白鸭. 妹子:那....有多喜欢? 我: 有很多很 ...
- JDK环境变量配置window
下面开始配置环境变量,右击[我的电脑]---[属性]-----[高级]---[环境变量],如图: 选择[新建系统变量]--弹出“新建系统变量”对话框,在“变量名”文本框输入“JAVA_HOME”,在 ...
- 安装Genymotion模拟器(第三方)
优势: 启动速度更快 注册账户,下载可用的系统镜像,就可以使用. 官方网站: https://www.genymotion.com/account/login/ 选择的版本是带VirtualB ...