bzoj 1396/2865: 识别子串 后缀自动机+线段树
水水的字符串题 ~
#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define M 500003
#define N 1000003
#define lson now<<1
#define rson now<<1|1
#define inf 1000000000
#define setIO(s) freopen(s".in","r",stdin) , freopen(s".out","w",stdout)
using namespace std;
int last,tot;
char str[N];
int rk[N],tax[N],A[N],minn[M<<2],minn2[M<<2];
struct data
{
int pre,cnt,len,id;
map<int,int>ch;
}t[N];
void Init() { last=tot=1; }
void extend(int c,int pos)
{
int np=++tot,p=last;
t[np].len=t[p].len+1,last=np;
for(;p&&!t[p].ch[c];p=t[p].pre) t[p].ch[c]=np;
if(!p) t[np].pre=1;
else
{
int q=t[p].ch[c];
if(t[q].len==t[p].len+1) t[np].pre=q;
else
{
int nq=++tot;
t[nq].len=t[p].len+1;
t[nq].id=t[q].id;
t[nq].ch=t[q].ch;
t[nq].pre=t[q].pre,t[q].pre=t[np].pre=nq;
for(;p&&t[p].ch[c]==q;p=t[p].pre) t[p].ch[c]=nq;
}
}
++t[np].cnt;
t[np].id=pos;
}
void build(int l,int r,int now)
{
minn[now]=minn2[now]=inf;
if(l==r) return;
int mid=(l+r)>>1;
if(l<=mid) build(l,mid,lson);
if(r>mid) build(mid+1,r,rson);
}
void update(int l,int r,int now,int L,int R,int v)
{
if(l>=L&&r<=R)
{
minn[now]=min(minn[now], v);
return;
}
int mid=(l+r)>>1;
if(L<=mid) update(l,mid,lson,L,R,v);
if(R>mid) update(mid+1,r,rson,L,R,v);
}
int query(int l,int r,int now,int p)
{
if(l==r) return minn[now];
int mid=(l+r)>>1,re=minn[now];
if(p<=mid) re=min(re, query(l,mid,lson,p));
else re=min(re, query(mid+1,r,rson,p));
return re;
}
void update2(int l,int r,int now,int L,int R,int v)
{
if(l>=L&&r<=R)
{
minn2[now]=min(minn2[now], v);
return;
}
int mid=(l+r)>>1;
if(L<=mid) update2(l,mid,lson,L,R,v);
if(R>mid) update2(mid+1,r,rson,L,R,v);
}
int query2(int l,int r,int now,int p)
{
if(l==r) return minn2[now];
int mid=(l+r)>>1, re=minn2[now];
if(p<=mid) re=min(re,query2(l,mid,lson,p));
else re=min(re, query2(mid+1,r,rson,p));
return re;
}
int main()
{
// setIO("input");
int n,i,j;
Init();
scanf("%s",str+1);
n=strlen(str+1);
for(i=1;i<=n;++i) extend(str[i]-'a',i);
for(i=1;i<=tot;++i) ++tax[t[i].len];
for(i=1;i<=tot;++i) tax[i]+=tax[i-1];
for(i=1;i<=tot;++i) rk[tax[t[i].len]--]=i;
build(1,n,1);
for(i=tot;i>1;--i)
{
int u=rk[i];
t[t[u].pre].cnt+=t[u].cnt;
t[t[u].pre].id=t[u].id;
if(t[u].cnt==1)
{
update(1,n,1,t[u].id-t[u].len+1,t[u].id-t[t[u].pre].len,t[u].id);
update2(1,n,1,t[u].id-t[t[u].pre].len,t[u].id,t[t[u].pre].len+1);
}
}
for(i=1;i<=n;++i) printf("%d\n",min(query(1,n,1,i)-i+1, query2(1,n,1,i)));
return 0;
}
bzoj 1396/2865: 识别子串 后缀自动机+线段树的更多相关文章
- BZOJ 1396&&2865 识别子串[后缀自动机 线段树]
Description 在这个问题中,给定一个字符串S,与一个整数K,定义S的子串T=S(i, j)是关于第K位的识别子串,满足以下两个条件: 1.i≤K≤j. 2.子串T只在S中出现过一次. 例如, ...
- bzoj1396&&2865 识别子串 后缀自动机+线段树
Input 一行,一个由小写字母组成的字符串S,长度不超过10^5 Output L行,每行一个整数,第i行的数据表示关于S的第i个元素的最短识别子串有多长. Sample Input agoodco ...
- BZOJ1396: 识别子串(后缀自动机 线段树)
题意 题目链接 Sol 后缀自动机+线段树 还是考虑通过每个前缀的后缀更新答案,首先出现次数只有一次,说明只有\(right\)集合大小为\(1\)的状态能对答案产生影响 设其结束位置为\(t\),代 ...
- BZOJ 1396 识别子串 (后缀自动机+线段树)
题目大意: 给你一个字符串S,求关于每个位置x的识别串T的最短长度,T必须满足覆盖x,且T在S中仅出现一次 神题 以节点x为结尾的识别串,必须满足它在$parent$树的子树中只有一个$endpos$ ...
- 【BZOJ1396】识别子串 - 后缀自动机+线段树
题意: Description Input 一行,一个由小写字母组成的字符串S,长度不超过10^5 Output L行,每行一个整数,第i行的数据表示关于S的第i个元素的最短识别子串有多长. 题解: ...
- 2018.12.23 bzoj2865&&1396: 字符串识别(后缀自动机+线段树)
传送门 卡空间差评! 题意简述:给一个字串,对于每个位置求出经过这个位置且只在字串中出现一次的子串的长度的最小值. 解法:先建出samsamsam,显然只有当sizep=1size_p=1sizep ...
- BZOJ 1396: 识别子串( 后缀数组 + 线段树 )
这道题各位大神好像都是用后缀自动机做的?.....蒟蒻就秀秀智商写一写后缀数组解法..... 求出Height数组后, 我们枚举每一位当做子串的开头. 如上图(x, y是height值), Heigh ...
- BZOJ 1396||2865 识别子串
这个不是题解,看不懂的,别看了 明明应该是会的,怎么还是写了6个小时呢... 把后缀数组.height数组.排名数组求出来,那么对于原串s的任意子串[x,y](表示第x个到第y个字符组成的子串,字符从 ...
- bzoj千题计划319:bzoj2865: 字符串识别(后缀自动机 + 线段树)
https://www.lydsy.com/JudgeOnline/problem.php?id=2865 同上一篇博客 就是卡卡空间,数组改成map #include<map> #inc ...
随机推荐
- Smart3D飞控&地方坐标系
资源 ContextCapture(原Smart3D)教程 | Wish3D 航测数据处理答疑55问!涉及CC.Pix4D.Photoscan.EPS等常见软件问题_云进行 飞控软件 Smart3D常 ...
- 如何利用 iTunes 把 m4a/wav 文件转成 MP3 格式
MAC技巧 | 如何利用 iTunes 把 m4a/wav 文件转成 MP3 格式 - 简书
- [二叉树算法]关于判断是否为BST的算法
//判断是否为BST 搜索树==二叉排序树 1.递归知最大最小值.2.先中序判是否单调 bool IsValidBST(BTNode *p,int low,int high){ if(p==NULL) ...
- 封装函数(累计和、K型、金字塔)
// 假设有个函数,只要传参数进去,就能统计累加的结果 function test($n){ if($n==1){ return 1; } return $n+test($n-1);}echo tes ...
- 优秀的java 社区
并发编程网 - ifeve.com InfoQ - 促进软件开发领域知识与创新的传播开源中国 - 找到您想要的开源项目,分享和交流IBM developerWorks 中国 : IBM's resou ...
- ELK搜索条件
1.要搜索一个确切的字符串,即精确搜索,需要使用双引号引起来:path:”/app/logs/nginx/access.log” 2.如果不带引号,将会匹配每个单词:uid token 3.模糊搜索: ...
- 【转载】Spring JdbcTemplate详解
JdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTempla ...
- GOF学习笔记1:术语
1.abstract class 抽象类定义了一个接口,把部分或全部实现留给了子类,不能实例化. 2.abstract coupling 抽象耦合如果一个类A引用了另一个抽象类B,那么就说A是抽象耦 ...
- jQuery效果之滑动
jQuery 滑动方法有三种:slideDown().slideUp().slideToggle(). jQuery slideDown() 方法用于向下滑动元素, 语法:$(selector).sl ...
- 异常-No suppression parameter found for notification
1 详细异常 Command Start is not currently available for execution. 关闭 kafka gateway 无法启动 java.lang.NullP ...