【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/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
随机推荐
- springboot + shiro 构建权限模块
权限模块基本流程 权限模块的基本流程:用户申请账号和权限 -->登陆认证 -->安全管控模块认证 -->调用具体权限模块(基于角色的权限控制) --> 登陆成功 -->访 ...
- ASP.NET MVC 5 伪静态之支持*.html路由
参考了例子 到自己实践还是有不少坑要踩,这种文章,你说它好还是不好呢 注意这里的版本是ASP.NET MVC 5 首页的URL为 http://localhost:58321/index.html ...
- .net mvc 迁移到 .netcore
迁移的时候发现,ef6 不能添加 到 .NET Standard2 的类库,因为不兼容, 6 以上的版本只能用于 .net 4.5 以上 只能用别的
- k8s 开源web操作平台
https://kuboard.cn/install/install-dashboard.html kuborad
- 多进程,多线程,使用sqlalchemy 创建引擎(单例模式),闭包装饰器承载数据库会话,装饰模型类的类方法
python 多进程,多线程,使用 sqlalchemy 对数据库进行操作 创建引擎 & 获取数据库会话: 使用类的方式,然后在对象方法中去创建数据库引擎(使用单例,确保只创建一个对象,方法里 ...
- 【转载】 C#使用String.Format拼接字符串
在C#程序开发过程中,很多时候会使用字符串拼接,最简单的字符串拼接操作就是所有的字符串使用加号+相加连接起来,但这种代码形式非常不适合代码维护阅读,尤其是拼接字符串语句比较复杂的时候,如拼接SQL语句 ...
- vue复制textarea文本域内容到粘贴板
vue实现复制内容到粘贴板 方案:找到textarea对象(input同样适用),获取焦点,选中textarea的所有内容,并调用document.execCommand("copy&q ...
- linux USB 编程
Linux USB架构 可以看出一个USB体系需要4个驱动:USB设备驱动(主要编写这部分),USB主控制器驱动,Gadget驱动,UDC驱动. USB主要有4个功能: MassStorage:大容量 ...
- for循环的耗时问题
结论——用变量来缓存数组长度,效率会更高
- MVC-区域(Area)
1.启用路由前的准备工作 1.Global.asax.cs中注册路由 public class MvcApplication : System.Web.HttpApplication { protec ...