SPOJ LCS2 Longest Common Substring II ——后缀自动机
后缀自动机裸题
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i<=k;--i)
#define inf 0x3f3f3f3f
#define ll long long
#define maxn 200005 struct sam{
char s[maxn];
int h[maxn],to[maxn],ne[maxn],en,ttt;
int dp[maxn][10],n;
int last,cnt,len,lth;
int go[maxn][26],l[maxn],fa[maxn];
void addedge(int a,int b)
{to[en]=b;ne[en]=h[a];h[a]=en++;}
void init()
{
memset(h,-1,sizeof h);en=0;
last=cnt=1;
// memset(go,0,sizeof go);
// memset(dp,0,sizeof dp);
}
void add(int x)
{
// printf("add %d\n",x);
int p=last,np=last=++cnt; l[np]=l[p]+1;
// printf("on point %d\n",np);
for (;p&&!go[p][x];p=fa[p]) go[p][x]=np;
if (!p) fa[np]=1;
else
{
int q=go[p][x];
if (l[q]==l[p]+1) fa[np]=q;
else
{
int nq=++cnt;
l[nq]=l[p]+1;
memcpy(go[nq],go[q],sizeof go[q]);
fa[nq]=fa[q];
fa[np]=fa[q]=nq;
for (;p&&go[p][x]==q;p=fa[p]) go[p][x]=nq;
}
}
}
void dfs(int o)
{
for (int i=h[o];i>=0;i=ne[i])
{
dfs(to[i]);
F(j,1,ttt)
dp[o][j]=max(dp[o][j],dp[to[i]][j]);
}
}
void solve()
{
scanf("%d",&n);
scanf("%s",s+1);
lth=len=strlen(s+1);
F(i,1,lth) add(s[i]-'a');
int i=0;
while (scanf("%s",s+1)!=EOF)
{
++i;
// scanf("%s",s+1);
len=strlen(s+1);
int now=1,t=0;
F(j,1,len)
{
int x=s[j]-'a';
if (go[now][x]) {t++;now=go[now][x];}
else
{
while (now&&!go[now][x]) now=fa[now];
if (!now) {t=0;now=1;}
else t=l[now]+1,now=go[now][x];
}
dp[now][i]=max(dp[now][i],t);
// for (int k=now;k;k=fa[k]) dp[k][i]=max(dp[k][i],t);
}
}
dfs(1);
int ans=0;ttt=i;
F(i,1,cnt) addedge(fa[i],i);
F(i,1,cnt)
{
int tmp=l[i];
F(j,2,ttt)
tmp=min(tmp,dp[i][j]);
ans=max(ans,tmp);
}
printf("%d\n",ans);
}
}SAM; int main()
{
SAM.init();
SAM.solve();
}
SPOJ LCS2 Longest Common Substring II ——后缀自动机的更多相关文章
- 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 (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- 【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)$个字符串的最长公共 ...
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- SPOJ LCS Longest Common Substring(后缀自动机)题解
题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...
随机推荐
- PL/SQL学习笔记(二)
select * from protype;select * from product;---笛卡尔连接查询(交叉连接)select * from protype,product;select * f ...
- SQL 时间日期函数
1.获取当前日期GetDate getdate()函数以datetime数据类型的格式返回当前SQLServer服务器所在计算机的日期和时间.其语法格式为getdate().返回值舍入到最近的秒小数部 ...
- Xcode编译工具
一.关于Other Linker Flags xcode中,在“Targets”选项下有Other Linker Flags选项,在这里可以填写xcode链接器的参数,如:-ObjC.-all_loa ...
- 用”人话”解释CNN —— 对单个特征图进行视觉化
转载自:http://nooverfit.com/wp/pycon-2016-tensorflow-研讨会总结-tensorflow-手把手入门-用人话解释cnn 首先什么是CNN? 其实, 用”人话 ...
- java在线聊天项目 swt可视化窗口Design 重新设计好友列表窗口 增加菜单栏
增加的菜单栏效果图如下: eclipse 中调整到 swt的design视图下 控件区域选择Menu Controls 将Menu Bar拖动到窗口标题栏 将Cascaded Menu拖动到Menu ...
- UVA-1220-Party at Hali-Bula && UVA-1218-Perfect Service(树形DP)
UVA-1220-Party at Hali-Bula 题意: 一个公司员工要举行聚会,要求任意一个人不能和他的直接上司同时到场,一个员工只有一个支系上司,现在求最多有多少人到场,并且方案是否唯一(紫 ...
- verilog RTL 编程实践之五
How to build and test a module 1.test have: generate .stimulus .check .respose 2.only one monitor ca ...
- leepcode - 5-16
7.有效的括号 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注 ...
- RESTful API批量操作的实现
要解决的问题 RESTful API对于批量操作存在一定的缺陷.例如资源的删除接口: DELETE /api/resourse/<id>/ 如果我们要删除100条数据怎么搞?难道要调用10 ...
- Django中重定向页面的时候使用命名空间
urls.py from django.urls import path from . import views app_name='front' urlpatterns = [ path('',vi ...