SPOJ bsubstr
题目大意:给你一个长度为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的更多相关文章
- 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 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- 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 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
- SPOJ GSS2 Can you answer these queries II
Time Limit: 1000MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description Being a ...
- SPOJ 3273
传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 Treap 版 //SPOJ 3273 //by Cydiater //2016.8.31 #include < ...
随机推荐
- html 涂改图片功能实现
网页源码直接贴了: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- GPU渲染和GDI
要实现这样一段逻辑,用GPU画3D图,用GDI画二维图元,怎么样效率高.相传Vista年代,是这样干的: 硬件渲染的东西在GPU上做完 读回CPU端 把GDI这些用软件渲染 两者混合 拷贝到显存显示 ...
- jquery实现自动补全邮箱地址
开始做的邮箱补全代码 //检查email邮箱 function isEmail(str) { if (str.indexOf("@") > 0) { return true; ...
- linux下的守护进程
关于守护进程,在此会介绍一下几种: 1.screen 2.supervisord(python) 一:Screen 开始使用Screen 简单来说,Screen是一个可以在多个进程之间多路复用一个物理 ...
- JS中this的值到底为何?
之前很久的时间,因为研究不深,对于this的值一直模模糊糊,不是很清楚,最近有空做了一些研究,终于彻底弄明白了this到底为何物. 首先, 先抛出一个定论:”在Javascript中,this关键字永 ...
- TYVJ 4354 多重背包二进制优化
直接放代码了 #include <cstdio> #include <cstring> #include <algorithm> using namespace s ...
- 数据获取以及处理Beta版本展示
产品描述 这个产品的目的是为了学霸网站提供后台数据获取以及处理操作.在alpha阶段基本调通的基础至上,我们希望在bate版本中加入对于问答对的处理,图片的获取等功能. 预期目标 在alpha阶段,我 ...
- [C# WPF]MoeEroViewer Developing Log
[C# WPF]MoeEroViewer Developing Log 1st - Base Document run on Https://github.com/Amarillys/MoeEroVi ...
- uva 12169
/* 巨大的斐波那契数列_________________________________________________________________________________ #inclu ...
- 王爽<<汇编语言>> 实验十四
;以"年/月/日 时:分:秒"的格式, 显示当前的日期, 时间 assume cs:code code segment main: out 70h,al ;告诉CMOS RAM将要 ...