SPOJ8222 NSUBSTR - Substrings(后缀自动机)
You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as the maximal number of times that some string with length x appears in S. For example for string 'ababa' F(3) will be 2 because there is a string 'aba' that occurs twice. Your task is to output F(i) for every i so that 1<=i<=|S|.
Input
String S consists of at most 250000 lowercase latin letters.
Output
Output |S| lines. On the i-th line output F(i).
Example
Input:
ababa Output:
3
2
2
1
1
为什么我的后缀自动机写的这么奇怪qwq。。
为什么泥萌都在写桶排??!!
这题不是dfs一下求出$right$集合来就完了么qwq、、
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = << , INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
char s[MAXN];
int N;
int f[MAXN], siz[MAXN], fa[MAXN], len[MAXN], ch[MAXN][], root = , last = , tot = ;
void insert(int x) {
int now = ++tot, pre = last; last = now;
siz[now] = ;
len[now] = len[pre] + ;
for(; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now;
if(!pre) fa[now] = root;
else {
int q = ch[pre][x];
if(len[q] == len[pre] + ) fa[now] = q;
else {
int nows = ++tot;
memcpy(ch[nows], ch[q], sizeof(ch[q]));
fa[nows] = fa[q]; fa[q] = fa[now] = nows;
len[nows] = len[pre] + ;
for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nows;
}
}
}
vector<int> v[MAXN];
void dfs(int x) {
for(int i = ; i < v[x].size(); i++) {
dfs(v[x][i]);
siz[x] += siz[v[x][i]];
}
f[len[x]] = max(f[len[x]], siz[x]);
}
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#else
//freopen("pow.in", "r", stdin);
//freopen("pow.out", "w", stdout);
#endif
scanf("%s", s + );
N = strlen(s + );
for(int i = ; i <= N; i++) insert(s[i] - 'a');
for(int i = ; i <= tot; i++) v[fa[i]].push_back(i);
dfs(root);
for(int i = ; i <= N; i++)
printf("%d\n", f[i]);
return ;
}
SPOJ8222 NSUBSTR - Substrings(后缀自动机)的更多相关文章
- SPOJ8222 NSUBSTR - Substrings 后缀自动机_动态规划
讲起来不是特别好讲.总之,如果 $dp[i+1]>=dp[i]$,故$dp[i]=max(dp[i],dp[i+1])$ Code: #include <cstdio> #inclu ...
- ●SPOJ 8222 NSUBSTR–Substrings(后缀自动机)
题链: http://www.spoj.com/problems/NSUBSTR/ 题解: 后缀自动机的水好深啊!懂不了相关证明,带着结论把这个题做了.看来这滩深水要以后再来了. 本题要用到一个叫 R ...
- SPOJ NSUBSTR Substrings 后缀自动机
人生第一道后缀自动机,总是值得纪念的嘛.. 后缀自动机学了很久很久,先是看CJL的论文,看懂了很多概念,关于right集,关于pre,关于自动机的术语,关于为什么它是线性的结点,线性的连边.许多铺垫的 ...
- SPOJ NSUBSTR Substrings ——后缀自动机
建后缀自动机 然后统计次数,只需要算出right集合的大小即可, 然后更新f[l[i]]和rit[i]取个max 然后根据rit集合短的一定包含长的的性质,从后往前更新一遍即可 #include &l ...
- SP8222 NSUBSTR - Substrings(后缀自动机+dp)
传送门 解题思路 首先建出\(sam\),然后把\(siz\)集合通过拓扑排序算出来.对于每个点只更新它的\(maxlen\),然后再从大到小\(dp\)一次就行了.因为\(f[maxlen-1]&g ...
- Substrings SPOJ - NSUBSTR (后缀自动机)
Substrings \[ Time Limit: 100ms\quad Memory Limit: 1572864 kB \] 题意 给出一个长度为 \(250000\) 的字符串,求出所有 \(x ...
- 【CF316G3】Good Substrings 后缀自动机
[CF316G3]Good Substrings 题意:给出n个限制(p,l,r),我们称一个字符串满足一个限制当且仅当这个字符串在p中的出现次数在[l,r]之间.现在想问你S的所有本质不同的子串中, ...
- SPOJ8222 NSUBSTR - Substrings
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- SPOJ8222 Substrings( 后缀自动机 + dp )
题目大意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.F(1)..F(Length(S)) 建出SAM, 然后求出Right, 求Right可以按拓扑序dp..Right ...
随机推荐
- linux shell之for循环
两种方式 第一种 for((i=1;i<10;i++)) do echo $i done 第二种 for i in {1..10} do echo $i done
- HTML代码中<%%>、<%=%>、<%:%>
<%%>之间可以写服务器端代码 比如 <% for(var i=0;i<10;i++){%> <%=%>获取后台的变量值,比如后台一个session[&quo ...
- XtraTabPage右键菜单(关闭当前页、关闭其它页、所有关闭的实现)
实现的需求: 用户习惯是一个不可忽略的东西,默认这版的dx的tab也木有右键操作,但用户习惯操作如浏览器都有右键关闭功能,故这里实现先dx的该功能 技术实现: (1)在winform的相应控件内,拖入 ...
- SpagoBI 和 开源ERP(iDempiere)整合入门
Created by 蓝色布鲁斯,QQ32876341,blog http://www.cnblogs.com/zzyan/ iDempiere官方中文wiki主页 http://wiki.idemp ...
- CSS3中的变形与动画(二)
CSS3动画 过渡属性transiton-property 早期在Web中要实现动画效果,都是依赖于JavaScript或Flash来完成.但在CSS3中新增加了一个新的模块transition,它可 ...
- linux c 遍历目录及文件
#include <dirent.h>void recovery_backend() { DIR * pdir ; struct dirent * pdirent; struct stat ...
- python 序列化,常用模块
生成器与迭代器 生成器 在 Python 中,使用了 yield 的函数被称为生成器(generator). 跟普通函数不同的是,生成器是一个返回迭代器的函数,只能用于迭代操作,更简单点理解生成器就是 ...
- 浅谈SQL Server中的事务日志(三)----在简单恢复模式下日志的角色
简介 在简单恢复模式下,日志文件的作用仅仅是保证了SQL Server事务的ACID属性.并不承担具体的恢复数据的角色.正如”简单”这个词的字面意思一样,数据的备份和恢复仅仅是依赖于手动备份和恢复.在 ...
- spring---FactoryBean与BeanFactory的区别
1.BeanFactory BeanFactory是IOC最基本的容器,负责生产和管理bean,它为其他具体的IOC容器提供了最基本的规范,例如DefaultListableBeanFactory, ...
- GitLab 数据备份和恢复
GitLab 备份 /opt/gitlab/bin/gitlab-rake gitlab:backup:create //只是备份各项目的数据 完成后会在默认路径下多出来备份的tar包! /var/o ...