题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数。

n<=250000

如:abaaa

输出:

4

2

1

1

1

spoj上的时限卡的太严,必须使用O(N)的算法那才能过掉,所以采用后缀自动机解决。

一个字符串出现的次数,即为后缀自动机中该字符串对应的节点的right集合的大小,right集合的大小等于子树中叶子节点的数目。

令dp[i]表示长度为i的字符串出现的最大次数。dp[i]可以通过在后缀链接树中从叶子节点到根节点依次求出。

最后,按长度从大到小,用dp[i]去更新dp[i-1]。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
#define MAXN 250005
#define MAXC 26
struct node
{
int go[MAXC],sufflink,step,right;
void clear()
{memset(go,,sizeof go);
sufflink=step=right=;
}
}tree[MAXN<<];
int dp[MAXN],cnt[MAXN],rk[MAXN<<],root,tot,cur,last,len,ans;
char s[MAXN];
void insert(int val)
{
int p,q;
tree[last].go[val]=++tot;
cur=tot;
tree[cur].clear();
tree[cur].step=tree[last].step+;
tree[cur].right++;
for(p=tree[last].sufflink;p&&tree[p].go[val]==;p=tree[p].sufflink)
tree[p].go[val]=cur;
if(p==)
tree[cur].sufflink=root;
else
{
q=tree[p].go[val];
if(tree[p].step+==tree[q].step)
tree[cur].sufflink=q;
else
{
tree[++tot].clear();
tree[tot]=tree[q];
tree[tot].right=;
tree[tot].step=tree[p].step+;
tree[cur].sufflink=tree[q].sufflink=tot;
for(;tree[p].go[val]==q;p=tree[p].sufflink)
tree[p].go[val]=tot;
}
}
last=cur;
}
void calc()
{
int temp;
for(int i=;i<=tot;i++)cnt[tree[i].step]++;
for(int i=;i<=len;i++)cnt[i]+=cnt[i-];
for(int i=;i<=tot;i++)rk[cnt[tree[i].step]--]=i;
for(int i=tot;i>;i--)
{
temp=tree[rk[i]].sufflink;
if(tree[rk[i]].right>dp[tree[rk[i]].step])dp[tree[rk[i]].step]=tree[rk[i]].right;
if(temp)tree[temp].right+=tree[rk[i]].right;
}
for(int i=len-;i>=;i--)
if(dp[i+]>dp[i])dp[i]=dp[i+];
for(int i=;i<=len;i++)
printf("%d\n",dp[i]);
}
int main()
{
while(~scanf("%s",s))
{
root=last=cur=tot=;
tree[].clear();
memset(dp,,sizeof dp);
memset(cnt,,sizeof cnt);
len=strlen(s);
for(int i=;i<len;i++)
insert(s[i]-'a');
calc();
}
return ;

SPOJ bsubstr的更多相关文章

  1. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  2. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  3. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  4. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  5. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  6. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  7. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...

  8. SPOJ GSS2 Can you answer these queries II

    Time Limit: 1000MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description Being a ...

  9. SPOJ 3273

    传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 Treap 版 //SPOJ 3273 //by Cydiater //2016.8.31 #include < ...

随机推荐

  1. C# Winform中无焦点状态下获取键盘输入或者USB扫描枪数据

    类文件: C#类文件 using System; using System.Collections.Generic; using System.Text; using System.Runtime.I ...

  2. UE4 中在 Actor 中动态 Create Component 与ChildActor 的 小笔记

    Note:旧版本的UE4 的Attach 和12.13版本有些不一样 创建Component: UCpp_MyComponent* temp_imageCom = NewObject<UCpp_ ...

  3. C# 类:类型 , 数学:类型

    类(类型):   //.Length:获取字符串的长度,返回int型                        //.Trim去除字符串前后的空格                        / ...

  4. go get安装第三方包的前提条件和步骤

    首先,你要找到第三方类包所在的网址, 例如:  http://github.com/go-sql-driver/mysql分为命令行自动安装 和 手动下载 然后安装. ( 欢迎加入go语言群: 218 ...

  5. 论文ei,sci检索,JCR-SCI分区,中科院分区连接

    https://jcr.incites.thomsonreuters.com/JCRJournalHomeAction.action?SID=B1-bQgax2FJ7EsyZ9muP6O5loc77S ...

  6. tensorflow资料补充(很棒)

    http://tensorfly.cn/tfdoc/get_started/introduction.html https://github.com/CreatCodeBuild/TensorFlow ...

  7. setTimeout的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. C#中ref,out

    out 关键字会导致参数通过引用来传递.这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化.若要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字 比如 ...

  9. PHP 与pdf文档 与条码

    必要的步骤 1.导入require_once "tcpdf/tcpdf.php"; 工具源码在demo中 2.$pdf = new TCPDF("P", &qu ...

  10. array_multisort 的详细使用方法

    1.如果第一个数组有相同的元素,那么后面对应位置的元素位置如何排序呢,请看下列代码$a1=array("Dog","Dog","Cat"); ...