[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 ...
随机推荐
- Apple Tree POJ - 2486
Apple Tree POJ - 2486 题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 树形dp.复杂度可能是O(玄学),不会超过$O(nk^2)$.(反正这 ...
- 【Visual Studio Code 】使用Visual Studio Code + Node.js搭建TypeScript开发环境
1.准备工作 Node.js Node.js - Official Site Visual Studio Code Visual Studio Code - Official Site 安装Node. ...
- [C#基础知识系列]专题十:全面解析可空类型[转]
原文链接 主要内容: 1:空合并操作符(?? 操作符) ??操作符也就是"空合并操作符",它代表的意思是两个操作数,如果左边的数不为null时,就返回左边的数,如果左边的数为nul ...
- AFNetworking2.5使用-转
来自:http://blog.csdn.net/daiyelang/article/details/38434023 官网下载2.5版本:http://afnetworking.com/ 此文章是基于 ...
- Web自动化测试框架-PO模式
Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架. 一.适用范围:传统Web功能自动化测试.H5功能自动化测试 ...
- Cenos7 切换单用户模式
CentOS 7在进入单用户的时候和6.x做了很多改变,下面让我们来看看如何进入单用户. 1.重启服务器,在选择内核界面使用上下箭头移动 2.选择内核并按“e” 3.修改参数 将rhgb quiet ...
- iOS 画环形图
由于新项目的的需求,需要画环形图,由于以前都没接触过这一类(我是菜鸟),去cocochina山找到了一个案例,个人觉得还可以,分享一下 github 地址https://github.com/zhou ...
- json三层解析(数组解析)
里面多了数组,所以用到了JOSNArray package com.xykj.weather; import java.io.BufferedReader; import java.io.IOExce ...
- R Programming week 3-Loop functions
Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...
- A/B宣言
作者:Dunne & Raby A B 肯定的 批判的 解决问题的 发现问题的 设计即流程 设计即方法 给出答案 问问题 为行业中服务 为社会服务 说明世界是怎样的 说明世界可能是怎样的 科 ...