[SPOJ8222]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|.

输入

String S consists of at most 250000 lowercase latin letters.

输出

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的更多相关文章

  1. SPOJ8222 Substrings( 后缀自动机 + dp )

    题目大意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.F(1)..F(Length(S)) 建出SAM, 然后求出Right, 求Right可以按拓扑序dp..Right ...

  2. 【spoj8222】 Substrings

    http://www.spoj.com/problems/NSUBSTR/ (题目链接) 题意 给出一个字符串S,令${F(x)}$表示S的所有长度为x的子串出现次数的最大值.求${F(1)..... ...

  3. SPOJ8222 NSUBSTR - Substrings

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. 【spoj8222】Substrings

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  5. Substrings(SPOJ8222) (sam(后缀自动机))

    You are given a string \(S\) which consists of 250000 lowercase latin letters at most. We define \(F ...

  6. SPOJ8222/NSUBSTR:Substrings——题解

    https://www.luogu.org/problemnew/show/SP8222#sub http://www.spoj.com/problems/NSUBSTR/ 翻译来自洛谷. 你得到一个 ...

  7. SPOJ8222 NSUBSTR - Substrings(后缀自动机)

    You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as ...

  8. SPOJ8222 NSUBSTR - Substrings 后缀自动机_动态规划

    讲起来不是特别好讲.总之,如果 $dp[i+1]>=dp[i]$,故$dp[i]=max(dp[i],dp[i+1])$ Code: #include <cstdio> #inclu ...

  9. 【SPOJ8222】Substrings (后缀自动机)

    题意: 给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值. 求F(1)..F(Length(S)) Length(S) <= 250000 思路:板子中st[x]定义为r ...

随机推荐

  1. Kali linux 2016.2(Rolling) 的详细安装(图文教程)附安装VMare Tools 增强工具

    写在前面的话 因读研期间,实验室团队需要,所以,接触上了Kali Linux,需去获得网络安全方面的数据,即数据和信息收集.以便为后续的数据处理和分析,准备! 用到hadoop和spark.机器学习等 ...

  2. 微软最新的Web服务器Katana发布了版本3

    Katana 项目入门 Howard Dierking 当 ASP.NET 首次在 2002 年发布时,时代有所不同. 那时,Internet 仍处于起步阶段,大约有 5.69 亿用户,每个用户平均每 ...

  3. 工具类学习-java实现邮件发送激活码

    问题:用java实现服务器发送激活码到用户邮件. 步骤一:如果是个人的话,确保在本地安装邮件服务器(易邮服务器)和邮件客户端(foxmail). 步骤二:导入jar包  mail.jar,其他的需要什 ...

  4. 原型模式及php实现

    原型模式: 通过复制已经存在的实例来返回新的实例,而不是新建实例,并且原型(被复制的实例)是可定制的:原型模式多用于创建复杂的或耗时的实例,这种情况下,复制一个已经存在的实例是程序运行更高效无疑是一种 ...

  5. mysql 字段包含某个字符的函数

    通过sql查询语句,查询某个字段中包含特定字符串: 例子:查询e_book表的types字段包含字符串"3",有下面4种方式 select * from e_book where ...

  6. nginx访问php程序相关配置

    server { listen *:80; charset utf-8; server_name roujiaxiaomowang.wanghaokun.com mowang.crucco.com; ...

  7. iOS---UICollectionView详解和常用API翻译

    UICollectionView 1.必须要设置布局参数 2.注册cell 用法类似于UITableView 类.自动实现重用,必须注册初始化. 使用UICollectionView必须实现UICol ...

  8. Sass的的使用一

    sass -v 检测是否安装 Sass 成功 gem update sass 更新 Sass gem uninstall sass 删除/卸载 Sass 的编译有多种方法: 1.命令编译2.GUI工具 ...

  9. asterisk-java ami5 分机状态,挂机原因之类的

    这些东西网上随便一找一大堆,也只是记录下自己找的.方便以后自己复制粘贴用. 最后为啦实现分机状态在web的实时更新,我选择啦使用websocket. //获得分机状态 public static St ...

  10. JavaSE-24 多线程

    学习要点 线程概述 Java中的多线程 线程状态 线程调度 线程同步 线程间通信 线程概述 1  进程 进程就是应用程序的执行实例. 进程特征: 动态性:动态产生,动态消亡.进程启动,系统为其分配资源 ...