1. 程式人生 > >SPOJ NSUBSTR(Substrings-字尾自動機統計串出現次數-Right集合&Parent樹の暴走)

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;
}