#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 500005
#define maxm 250005
using namespace std; int n,tot,root,last,f[maxm],fa[maxn],son[maxn][],dist[maxn],ri[maxn],sum[maxm],tmp[maxn];
char st[maxm];
struct Tsegment{
void prepare(){tot=root=last=; memset(dist,,sizeof(dist)); memset(sum,,sizeof(sum));}
int newnode(int x){
dist[++tot]=x; return tot;
}
void add(int x){
int p=last,np=newnode(dist[last]+); last=np;
for (;p&&!son[p][x];p=fa[p]) son[p][x]=np;
if (p==) fa[np]=root;
else{
int q=son[p][x];
if (dist[p]+==dist[q]) fa[np]=q;
else{
int nq=newnode(dist[p]+);
memcpy(son[nq],son[q],sizeof(son[q]));
fa[nq]=fa[q],fa[q]=fa[np]=nq;
for (;p&&son[p][x]==q;p=fa[p]) son[p][x]=nq;
}
}
}
}SAM; int main(){
scanf("%s",st+),n=strlen(st+);
SAM.prepare();
for (int i=;i<=n;i++) SAM.add(st[i]-'a');
last=root;
for (int i=;i<=n;i++) last=son[last][st[i]-'a'],ri[last]=;
memset(sum,,sizeof(sum));
for (int i=;i<=tot;i++) sum[dist[i]]++;
for (int i=;i<=n;i++) sum[i]+=sum[i-];
for (int i=;i<=tot;i++) tmp[sum[dist[i]]--]=i;
memset(f,,sizeof(f));
for (int i=tot;i>=;i--){
int x=tmp[i];
if (fa[x]) ri[fa[x]]+=ri[x];
}
for (int i=;i<=tot;i++) f[dist[i]]=max(f[dist[i]],ri[i]);
for (int i=n-;i>=;i--) f[i]=max(f[i],f[i+]);
for (int i=;i<=n;i++) printf("%d\n",f[i]);
return ;
}

题目链接:http://www.spoj.com/problems/NSUBSTR/

题目大意:给定一个长度为n的字符串,n<=250000,求f[i],i属于[1,n],f[i]表示在给定字符串中长度为i的子串的最多出现次数。

做法:初看此题,我也是一脸懵逼,后来发现出现次数与后缀自动机中的right集合大小有关,这题主要就是如何求right集合的大小,即该点表示的状态的右端点的个数(即出现次数),初始时我们从root开始匹配全串,途径的节点我们设其right为1,其余的节点设为0,某个节点的right就是parent树中以它为根的子树中的right值的和,一次dp即可。注解:parent树是我们记录的fa数组所构成的树,该步骤具体细节见代码。

dist表示SAM上该节点所表示的状态所能代表的最长的字符串长度,我们用right【i】去更新f【dist【i】】即可,最后用一次类似前缀和的算法,求一次后缀最值,显然,长度越小,出现次数一定不会减少。最后输出f数组即可。

后缀自动机。

【spoj8222】Substrings的更多相关文章

  1. 【spoj8222】 Substrings

    http://www.spoj.com/problems/NSUBSTR/ (题目链接) 题意 给出一个字符串S,令${F(x)}$表示S的所有长度为x的子串出现次数的最大值.求${F(1)..... ...

  2. 【SPOJ8222】Substrings (后缀自动机)

    题意: 给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值. 求F(1)..F(Length(S)) Length(S) <= 250000 思路:板子中st[x]定义为r ...

  3. 【SPOJ】Substrings(后缀自动机)

    [SPOJ]Substrings(后缀自动机) 题面 Vjudge 题意:给定一个长度为\(len\)的串,求出长度为1~len的子串中,出现最多的出现了多少次 题解 出现次数很好处理,就是\(rig ...

  4. 【SPOJ】Substrings

    出现次数很好处理,就是 \(right/endpos\) 集合的大小 那么,直接构建 \(SAM\) 求出每个位置的\(right\)集合大小 直接更新每个节点的\(longest\)就行了 最后短的 ...

  5. 【POJ1226】Substrings(后缀数组,二分)

    题意: n<=10,len<=100 思路: 只有一个字符串的时候特判一下 #include<cstdio> #include<cstring> #include& ...

  6. 【UVA10829】 L-Gap Substrings (后缀数组)

    Description If a string is in the form UVU, where U is not empty, and V has exactly L characters, we ...

  7. 【POJ3415】 Common Substrings(后缀数组|SAM)

    Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤ ...

  8. 【SPOJ】Distinct Substrings(后缀自动机)

    [SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...

  9. 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)

    [SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...

随机推荐

  1. WEBAPI 怎么保证安全

    先记录下   客户端登陆,服务端验证然后生成session,客户端每次调用接口的时候都把session带过去.服务端路由请求的时候都去验证session. 另外一种app的做法是,参数+用户名密码做s ...

  2. [转]各种有用的PHP开源库精心收集

    FROM : http://my.oschina.net/caroltc/blog/324024 1.html2ps and html2pdf    下载地址: http://www.tufat.co ...

  3. Dell 服务器做Raid

    Dell 服务器做Raid DELL R720 服务器 RAID阵列卡配置介绍 (H310) 关于 RAID 5 与热备份(Hot Spare) 在不同RAID组间使用热备盘——Global Hot ...

  4. mac 10.9.4下配置apache

    mac 10.9.x已经自带了apache,可按如下步骤开启: 1.启动 sudo apachectl start 启动后,访问 http://localhost/ 应该能看到"It wor ...

  5. 前后端分离工具之ftl-server

    文章来源:https://www.npmjs.com/package/ftl-server 源代码可参考:https://github.com/szmtcjm/ftl-server/blob/mast ...

  6. 细细品味Storm_Storm简介及安装

    Storm是由专业数据分析公司BackType开发的一个分布式实时数据处理软件,可以简单.高效.可靠地处理大量的数据流.Twitter在2011年7月收购该公司,并于2011年9月底正式将Storm项 ...

  7. .net RPC框架选型

    近期开始研究分布式架构,会涉及到一个最核心的组件:RPC(Remote Procedure Call Protocol).这个东西的稳定性与性能,直接决定了分布式架构系统的好坏.RPC技术,我们的产品 ...

  8. js的this什么时候会出现报错

    var aa ={ name:"boy", age:, like: function(){ console.log(this.name); } } //aa.like();//这样 ...

  9. Verilog代码规范I

    Verilog代码规范I "规范"这问题 "规范"这个富含专业气息的词汇(个人感觉),其实规范这种东西,就是大家都约定熟成的东西,一旦你不遵守这个东西,专业人士 ...

  10. 东大OJ-1588: Routing Table

    题目描述 In the computer network, a Router is a device which finds an optimal way to transmit the datagr ...