spoj 8222 Substrings



【分析】

看了两个半小时的论文...终于懂一点了(所以我以前压根就没懂SAM啊)

张天扬刷新了我对SAM的认识(以后再也不乱找奇奇怪怪的课件了)

推荐http://download.csdn.net/detail/acvay/8950959(张天扬sam论文)


然而spoj太慢了还限制0.1s根本卡不动= =(谁每天没事了和spoj这鬼畜网站卡常数)



【代码】(再度光荣TLE)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(register int i=j;i<=k;i++)
using namespace std;
const int mxn=500005;
char s[mxn];
int t[mxn],rig[mxn],f[mxn],b[mxn];  //rig数组的大小 
int n,m,len,p,q,np,nq,root,tot,ans,cnt;
int son[mxn][27],pre[mxn],step[mxn];
inline void sam()
{
	scanf("%s",s+1);
	len=strlen(s+1);
	np=root=tot=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;
			fo(j,1,26) son[nq][j]=son[q][j];
			pre[nq]=pre[q];
			pre[q]=pre[np]=nq;
			while(p && son[p][c]==q)
			  son[p][c]=nq,p=pre[p];
		}
	}
}
int main()
{
	sam();
	p=root;
	fo(i,1,len)
	{
		int c=s[i]-'a'+1;
		p=son[p][c],rig[p]++;
	}
	fo(i,1,tot) b[step[i]]++;
	fo(i,1,len) b[i]+=b[i-1];
	fo(i,1,tot) t[b[step[i]]--]=i;  //基数排序:保证顺序从树底到树顶 
	for(int i=tot;i;i--) rig[pre[t[i]]]+=rig[t[i]];
	fo(i,1,tot) f[step[i]]=max(f[step[i]],rig[i]);
	for(int i=len;i;i--) f[i]=max(f[i+1],f[i]);
	fo(i,1,len) printf("%d\n",f[i]);
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值