SPOJ NSUBSTR(Substrings-后缀自动机统计串出现次数-Right集合&Parent树の暴走)

8222. Substrings

Problem code: NSUBSTR

You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as the maximal number of times that some string with length x appears in S. For example for string 'ababa' F(3) will be 2 because there is a string 'aba' that occurs twice. Your task is to output F(i) for every i so that 1<=i<=|S|.

Input

String S consists of at most 250000 lowercase latin letters.

Output

Output |S| lines. On the i-th line output F(i).

Example

Input:
ababa

Output:
3
2
2
1
1


后缀自动机入门用。。。

有关后缀自动机的话题省略100字、、(我能告诉你我其实也没懂吗)

树边的结点表示后缀所在Right集合的点=叶结点(定义)
故把每个结点的|Right|(集合大小)求出来更新答案

得到 结点代表的最长后缀=k的最大出现次数

→     结点代表的最长后缀≥k的最大出现次数




#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (600000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
struct node
{
	int pre,ch[26];
	int step;
	char c;
	node(char c):c(c){pre=step=0;memset(ch,0,sizeof(ch));	}
	node():c(0){pre=step=0;memset(ch,0,sizeof(ch));	}
}a[MAXN];
int last=0,total=0;
void ins(char c)
{
	int np=++total;a[np].c=c,a[np].step=a[last].step+1;
	int p=last;
	for(;!a[p].ch[c];p=a[p].pre) a[p].ch[c]=np;
	if (a[p].ch[c]==np) a[np].pre=p;
	else 
	{
		int q=a[p].ch[c];
		if (a[q].step>a[p].step+1)
		{
			int nq=++total;a[nq]=a[q];a[nq].step=a[p].step+1;
			a[np].pre=a[q].pre=nq;
			for(;a[p].ch[c]==q;p=a[p].pre) a[p].ch[c]=nq;			
		}else a[np].pre=q;
	}
	last=np;
}
char dfsc[MAXN]="";
void dfs(int x,int l)
{
	dfsc[l]=a[x].c+'a';
	dfsc[l+1]=0;
//	if (dfsc[1]) printf("%s\n",dfsc+1);
//	Rep(i,26) if (a[x].ch[i]) dfs(a[x].ch[i],l+1);
}
char s[MAXN];
int n,t[MAXN]={0},r[MAXN]={0},v[MAXN]={0},ans[MAXN]={0};
int main()
{
//	freopen("spojNSUBSTR.in","r",stdin);
	scanf("%s",s+1);int n=strlen(s+1);
	For(i,n) ins(s[i]-'a');
//	dfs(0,0);
	For(i,total) t[a[i].step]++;
	For(i,n) t[i]+=t[i-1];
	For(i,total) r[t[a[i].step]--]=i;
//	For(i,total) cout<<r[i]<<' ';cout<<endl;
	int now=0;
	For(i,n) v[now=a[now].ch[s[i]-'a']]++;
	ForD(i,total)
	{
		int now=r[i];
		ans[a[now].step]=max(ans[a[now].step],v[now]);
		v[a[now].pre]+=v[now];		
	}	
	
	ForD(i,n-1) ans[i]=max(ans[i],ans[i+1]);
	For(i,n) printf("%d\n",ans[i]);
	
	return 0;
}








发布了1040 篇原创文章 · 获赞 42 · 访问量 113万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览