水水的字符串题 ~

#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: 识别子串 后缀自动机+线段树的更多相关文章

  1. BZOJ 1396&&2865 识别子串[后缀自动机 线段树]

    Description 在这个问题中,给定一个字符串S,与一个整数K,定义S的子串T=S(i, j)是关于第K位的识别子串,满足以下两个条件: 1.i≤K≤j. 2.子串T只在S中出现过一次. 例如, ...

  2. bzoj1396&&2865 识别子串 后缀自动机+线段树

    Input 一行,一个由小写字母组成的字符串S,长度不超过10^5 Output L行,每行一个整数,第i行的数据表示关于S的第i个元素的最短识别子串有多长. Sample Input agoodco ...

  3. BZOJ1396: 识别子串(后缀自动机 线段树)

    题意 题目链接 Sol 后缀自动机+线段树 还是考虑通过每个前缀的后缀更新答案,首先出现次数只有一次,说明只有\(right\)集合大小为\(1\)的状态能对答案产生影响 设其结束位置为\(t\),代 ...

  4. BZOJ 1396 识别子串 (后缀自动机+线段树)

    题目大意: 给你一个字符串S,求关于每个位置x的识别串T的最短长度,T必须满足覆盖x,且T在S中仅出现一次 神题 以节点x为结尾的识别串,必须满足它在$parent$树的子树中只有一个$endpos$ ...

  5. 【BZOJ1396】识别子串 - 后缀自动机+线段树

    题意: Description Input 一行,一个由小写字母组成的字符串S,长度不超过10^5 Output L行,每行一个整数,第i行的数据表示关于S的第i个元素的最短识别子串有多长. 题解: ...

  6. 2018.12.23 bzoj2865&&1396: 字符串识别(后缀自动机+线段树)

    传送门 卡空间差评! 题意简述:给一个字串,对于每个位置求出经过这个位置且只在字串中出现一次的子串的长度的最小值. 解法:先建出samsamsam,显然只有当sizep=1size_p=1sizep​ ...

  7. BZOJ 1396: 识别子串( 后缀数组 + 线段树 )

    这道题各位大神好像都是用后缀自动机做的?.....蒟蒻就秀秀智商写一写后缀数组解法..... 求出Height数组后, 我们枚举每一位当做子串的开头. 如上图(x, y是height值), Heigh ...

  8. BZOJ 1396||2865 识别子串

    这个不是题解,看不懂的,别看了 明明应该是会的,怎么还是写了6个小时呢... 把后缀数组.height数组.排名数组求出来,那么对于原串s的任意子串[x,y](表示第x个到第y个字符组成的子串,字符从 ...

  9. bzoj千题计划319:bzoj2865: 字符串识别(后缀自动机 + 线段树)

    https://www.lydsy.com/JudgeOnline/problem.php?id=2865 同上一篇博客 就是卡卡空间,数组改成map #include<map> #inc ...

随机推荐

  1. Bipartite Checking CodeForces - 813F (线段树按时间分治)

    大意: 动态添边, 询问是否是二分图. 算是个线段树按时间分治入门题, 并查集维护每个点到根的奇偶性即可. #include <iostream> #include <sstream ...

  2. 记一次纯sqlite数据库的小项目开发经历

    sqlite有哪些坑 1.支持的数据量级:根据SQLite的官方提示:http://www.sqlite.org/limits.htmlSQLIte数据库最大支持128TiB(140 terabyte ...

  3. dB分贝计算

    1. 定义 dB-表征相对值的大小的单位,即两个电.声功率之比或者电流.电压.音量之比,是一种测相对大小的单位. 1.1 电.声功率之比——10lg(x/y) x.y分别表示两个欲比较的功率值. 例如 ...

  4. Feign 客户端调用错误

    1.@RequestBody 必须要写在实现接口中 2.Feign 客户端调用的时候如果有参数的话,默认是发送post请求 3.服务接口中的请求参数必须要加上@RequestParam("r ...

  5. 数据库中间件之mycat读写分离

    mycat核心概念 逻辑库 mycat中定义.管理的数据库 逻辑表 逻辑库中包含的需分库分表存储的表 datanode 数据节点(分片节点),逻辑表分片的存放节点 datahost 数据主机(节点主机 ...

  6. mysql 系统变量

    show variables; ---------------------------------+-------------------------------------------------- ...

  7. opencv Cascade Classifier Training中 opencv_annotation命令

    在win7 cmd中试了官方的指令opencv_annotation --annotations=/path/to/annotations/file.txt --images=/path/to/ima ...

  8. css中哪些属性会脱离文档流

    一共有三个属性会使元素脱离文档流: (1)浮动 (2)绝对定位 (3)固定定位 元素脱离文档流会导致父元素高度塌陷,要修复高度塌陷问题,可以将子元素与父元素放在同一个BFC中

  9. Python 虚拟空间的使用

    使用虚拟环境, 可以将当前项目所使用的依赖与电脑中其他 Python 项目的依赖区分开, 避免依赖版本不匹配带来的问题, 同时也可以防止项目依赖被不当更新. mkdir myproject cd my ...

  10. Python面向对象Day1

    一.面向对象初始 面向过程变成属于流水式 面向对象是一种思想 结构上理解面向对象:两部分 class A: # 类 name = '小明' # 静态属性,静态变量,静态字段,或者属性.变量.字段 de ...