[SPOJ8222]Substrings
[SPOJ8222]Substrings
试题描述
输入
输出
Output |S| lines. On the i-th line output F(i).
输入示例
ababa
输出示例
数据规模及约定
见“输入”
题解
构造后缀自动机,然后用每个节点 i 的 righti 集合大小更新 f[Max[i]],然后再用每个 f[i] 更新 f[i-1]。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 500010
#define maxa 26 int n;
int ToT, rt, last, ch[maxn][maxa], par[maxn], Max[maxn], siz[maxn];
void extend(int x) {
int p = last, np = ++ToT; Max[np] = Max[p] + 1; siz[np] = 1;
last = np;
while(p && !ch[p][x]) ch[p][x] = np, p = par[p];
if(!p){ par[np] = rt; return ; }
int q = ch[p][x];
if(Max[q] == Max[p] + 1){ par[np] = q; return ; }
int nq = ++ToT; Max[nq] = Max[p] + 1;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
par[nq] = par[q];
par[q] = nq;
par[np] = nq;
while(p && ch[p][x] == q) ch[p][x] = nq, p = par[p];
return ;
} int f[maxn];
char Str[maxn];
int sa[maxn], Ws[maxn];
void build() {
for(int i = 1; i <= ToT; i++) Ws[n-Max[i]]++;
for(int i = 1; i <= n; i++) Ws[i] += Ws[i-1];
for(int i = ToT; i; i--) sa[Ws[n-Max[i]]--] = i;
for(int i = 1; i <= ToT; i++) siz[par[sa[i]]] += siz[sa[i]];
return ;
} int main() {
scanf("%s", Str); n = strlen(Str);
rt = last = ToT = 1;
for(int i = 0; i < n; i++) extend(Str[i] - 'a'); build();
for(int i = 1; i <= ToT; i++) f[Max[i]] = max(f[Max[i]], siz[i]);
for(int i = n; i > 1; i--) f[i-1] = max(f[i], f[i-1]); for(int i = 1; i <= n; i++) printf("%d\n", f[i]); return 0;
}
[SPOJ8222]Substrings的更多相关文章
- SPOJ8222 Substrings( 后缀自动机 + dp )
题目大意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.F(1)..F(Length(S)) 建出SAM, 然后求出Right, 求Right可以按拓扑序dp..Right ...
- 【spoj8222】 Substrings
http://www.spoj.com/problems/NSUBSTR/ (题目链接) 题意 给出一个字符串S,令${F(x)}$表示S的所有长度为x的子串出现次数的最大值.求${F(1)..... ...
- SPOJ8222 NSUBSTR - Substrings
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 【spoj8222】Substrings
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- Substrings(SPOJ8222) (sam(后缀自动机))
You are given a string \(S\) which consists of 250000 lowercase latin letters at most. We define \(F ...
- SPOJ8222/NSUBSTR:Substrings——题解
https://www.luogu.org/problemnew/show/SP8222#sub http://www.spoj.com/problems/NSUBSTR/ 翻译来自洛谷. 你得到一个 ...
- SPOJ8222 NSUBSTR - Substrings(后缀自动机)
You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as ...
- SPOJ8222 NSUBSTR - Substrings 后缀自动机_动态规划
讲起来不是特别好讲.总之,如果 $dp[i+1]>=dp[i]$,故$dp[i]=max(dp[i],dp[i+1])$ Code: #include <cstdio> #inclu ...
- 【SPOJ8222】Substrings (后缀自动机)
题意: 给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值. 求F(1)..F(Length(S)) Length(S) <= 250000 思路:板子中st[x]定义为r ...
随机推荐
- 转-iOS开发系列--地图与定位
来自: http://www.cnblogs.com/kenshincui/p/4125570.html#autoid-3-4-0 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功 ...
- Xml学习笔记(1)
不同的xml文档构可能要用到不同的方法进行解析这里用到的是例如<student name="张三" id="1" sex="男"/&g ...
- AJPFX关于增强for的概述和使用(foreach)
增强for的概述和使用(foreach)1.增强for的概述和使用(foreach) 格式: for(数组或者Collection集合中元素 ...
- 腾讯云COS对象存储的简单使用
叮当哥之前买了一年的腾讯云服务器,昨日偶然发现腾讯云送了叮当哥半年的cos对象存储服务器,于是就撸起袖子传了几张珍藏的高清大图上去,现将其上传的简单使用步骤总结一波(其它操作参加官方SDK文档API) ...
- [BZOJ2002][Hnoi2010]Bounce弹飞绵羊 LCT
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 建图,每次往后面跳就往目标位置连边,将跳出界的点设为同一个点.对于修改操作发现可以用 ...
- WordPress更改固定链接出现404
新浪SAE的前端采用的是nginx,nginx是不识别.htaccess的. 最后学习了新浪SAE官方教程——应用配置模块 – AppConfig终于把问题解决! 1.修改你SAE SDK站点目录下的 ...
- jboss中JVM监控
1)打开 http://server-name-or-ip/jmx-console/HtmlAdaptor2)在 jboss.system 节点找到 type=ServerInfo ,点击进入3)找到 ...
- jQuery 的DOM操作
DOM创建节点及节点属性 创建元素:document.createElement设置属性:setAttribute添加文本:innerHTML加入文档:appendChild append()前面是被 ...
- MySQL for Mac 终端操作说明
mysql for mac 终端操作说明MySQL服务开启Mac版mysql可以从设置里启动服务: 如果想要在终端(Terminal)中操作mysql,需要先添加mysql路径,在此以zsh为例: # ...
- PC端浏览器定位
第一: PC端浏览器定位(纯前端)浏览器定位 :这里用了两种 ,一种是Html5自带的方法 另一种是引用了百度api ,百度api 的使用有三种:1 浏览器定位2 ip定位3 SDK辅助定位引用百度 ...