题目大意:给你一个长度为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. sql文件批量导入mysql数据库

    有一百多个sql文件肿么破?一行一行地导入数据库肯定是极其愚蠢的做法,但是我差点就这么做了... 网上首先找到的方法是:写一个xxx.sql文件,里边每一行都是source *.sql ...,之后再 ...

  2. Win10/UWP新特性—Drag&Drop 拖出元素到其他App

    在以前的文章中,写过微软新特性Drag&Drop,当时可能由于处于Win10预览版,使用的VS也是预览版,只实现了从桌面拖拽文件到UWP App中,没能实现从UWP拖拽元素到Desktop A ...

  3. c# 常量,变量

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. Less 语法特性

                                     ——(原创翻译:译者添加部分解释和代码运行结果) ♥在线Less编译器:LESS2CSS <一>综述 Less作为CSS的 ...

  5. 黑苹果-IOS学习的开始

    深知安装黑苹果的不易,在这里写一下关于我的Thinkpad E430c安装黑苹果教程(Mac版本:Yosemite 10.10.4),希望能够帮助有需要的朋友. 首先贴上我的电脑配置报表: ----- ...

  6. 一个简单的python程序

    假设我们有这么一项任务:简单测试局域网中的电脑是否连通.这些电脑的ip范围从192.168.0.101到192.168.0.200. import subprocesscmd="cmd.ex ...

  7. 关于discuz“终于解决“头像保存过程中发生网络错误,请重试"”的解决方法

    1 php.ini里面allow_url_fopen = On2 将php.ini中的;upload_tmp_dir = 该行的注释符,即前面的分号“:”去掉,使该行在php.ini文档中起作用.up ...

  8. IOS OC 多任务定时器 NSRunLoop 管理 NSTimer

    下面有两种做法 1.使用日期组件 NSDateComponents 2.使用NSString 生成一个日期 //  创建一个日历对象 NSCalendar *calendar = [NSCalenda ...

  9. 6.Counting Point Mutations

    Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colore ...

  10. JS函数的上下文环境

    var i=1; var fn1=function(){ console.log(i); } var fn2=function(){ var i=2; fn1(); } fn2();      // ...