bzoj 1396 识别子串

1396: 识别子串

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 396   Solved: 252
[ Submit][ Status][ Discuss]

Description

Input

一行,一个由小写字母组成的字符串S,长度不超过10^5

Output

L行,每行一个整数,第i行的数据表示关于S的第i个元素的最短识别子串有多长.

Sample Input

agoodcookcooksgoodfood

Sample Output

1
2
3
3
2
2
3
3
2
2
3
3
2
1
2
3
3
2
1
2
3

4





【分析】

吕老板说SAM比sa更无脑的时候我的内心是绝望的(到底哪里无脑了...)

——题解摘自 Candy?

建SAM,|Right|=1的可以作为识别子串哦

|Right(s)|=1 出现位置就是Max(s)

考虑它可以作为哪些位置的识别子串

令r=Max(s),l=Max(s)-Max(fa)

[1,l-1]可以,贡献为r-i+1

[l,r]可以,贡献为r-l+1

用两颗线段树就行了

肝了两节课啊...疲劳



【代码】

//bzoj 1396 识别子串
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define inf 1e9+7
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=200005;
char s[mxn];
bool bo[mxn]; //有无pre反向边 
int n,m,p,q,np,nq,len,tot,root;
int step[mxn],pre[mxn],son[mxn][30];
struct segtree  //A:维护min(key[i]),B:维护min(key[i]+i)
{
	struct node
	{
		int l,r,mn;  //mn:只用来pushdown 
		node():mn(inf){}
	}t[mxn<<1];
	inline void build(int l,int r,int num)
	{
		t[num].l=l,t[num].r=r;
		if(l==r) return;
		int mid=l+r>>1;
		build(l,mid,num<<1);
		build(mid+1,r,num<<1|1);
	}
	inline void move(int num,int c)
	{
		t[num].mn=min(t[num].mn,c);
	}
	inline void pushdown(int num)
	{
		if(t[num].mn!=inf)
		  move(num<<1,t[num].mn),move(num<<1|1,t[num].mn);
		t[num].mn=inf;
	}
	inline void change(int num,int L,int R,int c)
	{
		if(L<=t[num].l && t[num].r<=R)
		{
			move(num,c);return;
		}
		pushdown(num);
		int mid=t[num].l+t[num].r>>1;
		if(L<=mid) change(num<<1,L,R,c);
		if(R>mid) change(num<<1|1,L,R,c);
	}
	inline int query(int num,int pos)
	{
		if(t[num].l==t[num].r) return t[num].mn;
		int mid=t[num].l+t[num].r>>1;
		pushdown(num);
		if(pos<=mid) return query(num<<1,pos);
		else return query(num<<1|1,pos);
	}
}A,B;
inline void sam()
{
	int i,j;
	scanf("%s",s+1);
	len=strlen(s+1);
	root=tot=np=1;
	fo(i,1,len)
	{
		int c=s[i]-'a'+1;
		p=np;
		step[np=(++tot)]=step[p]+1;
		while(p && !son[p][c])
		  son[p][c]=np,p=pre[p];
		if(!p)
		{
			pre[np]=root;
			continue;
		}
		q=son[p][c];
		if(step[q]==step[p]+1)
		  pre[np]=q;
		else
		{
			step[nq=(++tot)]=step[p]+1;
			memcpy(son[nq],son[q],sizeof son[q]);
			pre[nq]=pre[q];
			pre[q]=pre[np]=nq;
			while(p && son[p][c]==q)
			  son[p][c]=nq,p=pre[p];
		}
	}
}
int main()
{
	int i,j;
	sam();
	fo(i,1,tot) bo[pre[i]]=1;
	A.build(1,len,1),B.build(1,len,1);
	fo(i,1,tot)
	  if(!bo[i])
	  {
	  	  int l=step[i]-step[pre[i]],r=step[i];
	  	  B.change(1,1,l-1,r+1);
	  	  A.change(1,l,r,r-l+1);
	  }
	fo(i,1,len)
	  printf("%d\n",min(A.query(1,i),B.query(1,i)-i));
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值