【SPOJ】8222. Substrings(后缀自动机)
http://www.spoj.com/problems/NSUBSTR/
题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值。求F(1)..F(Length(S))
这题做法:
首先建立字符串的后缀自动机。
所以对于一个节点$s$,长度范围为$[Min(s), Max(s)]$,出现次数是$|Right(s)|$,那么我们用$|Right(s)|$去更新$f(Max(s))$,最后用$f(i)$更新$f(i-1)$即可。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } struct sam {
static const int N=1000005;
int c[N][26], l[N], f[N], root, last, cnt;
sam() { cnt=0; root=last=++cnt; }
void add(int x) {
int now=last, a=++cnt; last=a;
l[a]=l[now]+1;
for(; now && !c[now][x]; now=f[now]) c[now][x]=a;
if(!now) { f[a]=root; return; }
int q=c[now][x];
if(l[q]==l[now]+1) { f[a]=q; return; }
int b=++cnt;
memcpy(c[b], c[q], sizeof c[q]);
l[b]=l[now]+1;
f[b]=f[q];
f[q]=f[a]=b;
for(; now && c[now][x]==q; now=f[now]) c[now][x]=b;
}
void build(char *s) {
int len=strlen(s);
rep(i, len) add(s[i]-'a');
}
}a; const int N=250005;
char s[N];
int f[N], r[1000005], t[N], b[1000005];
void getans(int len) {
int *l=a.l, *p=a.f, cnt=a.cnt;
for(int now=a.root, i=0; i<len; ++i) now=a.c[now][s[i]-'a'], ++r[now];
for1(i, 1, cnt) ++t[l[i]];
for1(i, 1, len) t[i]+=t[i-1];
for1(i, 1, cnt) b[t[l[i]]--]=i;
for3(i, cnt, 1) r[p[b[i]]]+=r[b[i]];
for1(i, 1, cnt) f[l[i]]=max(f[l[i]], r[i]);
for3(i, len, 1) f[i]=max(f[i+1], f[i]);
for1(i, 1, len) printf("%d\n", f[i]);
}
int main() {
scanf("%s", s);
a.build(s);
getans(strlen(s));
return 0;
}
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
【SPOJ】8222. Substrings(后缀自动机)的更多相关文章
- spoj 8222 Substrings (后缀自动机)
		spoj 8222 Substrings 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) 解题思路:我们构造S的SAM,那么对于 ... 
- SPOJ NSUBSTR Substrings 后缀自动机
		人生第一道后缀自动机,总是值得纪念的嘛.. 后缀自动机学了很久很久,先是看CJL的论文,看懂了很多概念,关于right集,关于pre,关于自动机的术语,关于为什么它是线性的结点,线性的连边.许多铺垫的 ... 
- SPOJ NSUBSTR Substrings ——后缀自动机
		建后缀自动机 然后统计次数,只需要算出right集合的大小即可, 然后更新f[l[i]]和rit[i]取个max 然后根据rit集合短的一定包含长的的性质,从后往前更新一遍即可 #include &l ... 
- spoj 1812 lcsII (后缀自动机)
		spoj 1812 lcsII (后缀自动机) 题意:求多个串的lcs,最多10个串,每个串最长10w 解题思路:后缀自动机.先建好第一个串的sam,然后后面的串拿上去跑(这个过程同前一题).sam上 ... 
- ●SPOJ 8222 NSUBSTR–Substrings(后缀自动机)
		题链: http://www.spoj.com/problems/NSUBSTR/ 题解: 后缀自动机的水好深啊!懂不了相关证明,带着结论把这个题做了.看来这滩深水要以后再来了. 本题要用到一个叫 R ... 
- SPOJ 8222. Substrings(后缀自动机模板)
		后缀自动机+dp. 后缀自动机主要是在functioner大牛那里学习的:http://blog.sina.com.cn/s/blog_70811e1a01014dkz.html 这道题是在No_st ... 
- Substrings SPOJ - NSUBSTR (后缀自动机)
		Substrings \[ Time Limit: 100ms\quad Memory Limit: 1572864 kB \] 题意 给出一个长度为 \(250000\) 的字符串,求出所有 \(x ... 
- POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀自动机)
		题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 确实比后缀数组快多了(废话→_→). \(Description\) 求两个字符串最长公共子串 ... 
- 【CF316G3】Good Substrings 后缀自动机
		[CF316G3]Good Substrings 题意:给出n个限制(p,l,r),我们称一个字符串满足一个限制当且仅当这个字符串在p中的出现次数在[l,r]之间.现在想问你S的所有本质不同的子串中, ... 
- Lexicographical Substring Search SPOJ - SUBLEX (后缀自动机)
		Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串 ... 
随机推荐
- 显示游戏FPS帧率的几种计算方式
			FPSDisplay.cs using UnityEngine; using System.Collections; public class FPSDisplay : MonoBehaviour { ... 
- Android中涉及到的焦点问题,focusable,clickable,enabled
			先摘抄下stackoverflow上一个启示了我的回答: try by Changing your code as: private OnClickListener saveButtonListene ... 
- Android系统示例分析之AccelerometerPlay
			程序界面: 创建 SimulationView , 实现接口 SensorEventListener , 实现接口中两个抽象方法 public void onSensorChanged(SensorE ... 
- Python 开发者的 6 个必备库,你都了解吗?
			无论你是正在使用 Python 进行快速开发,还是在为 Python 桌面应用制作原生 UI ,或者是在优化现有的 Python 代码,以下这些 Python 项目都是应该使用的. Python那些事 ... 
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
			Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ... 
- 10 分钟实现一个自己的server监控器
			需求 近期须要给自己的server加入监控器.目的是监控server的内存.CPU.磁盘占用率,资源占用率过高的话能给自己发个提醒.当前主流的平台通常会提供邮件.短息.甚至会提供微信提醒,只是这类提醒 ... 
- Win10 环境安装tesseract-ocr 4.00并配置环境变量
			Tesseract-OCR的Training简明教程 https://blog.csdn.net/blueheart20/article/details/53207176 一.安装: 选择对应版本,h ... 
- 小程序 wx.request ajax示例
			简单示例 https://developers.weixin.qq.com/miniprogram/dev/api/network-request.html wx.request({ method: ... 
- C++代码优化方法总结
			C++代码优化方法总结 优化是一个非常大的主题,本文并不是去深入探讨性能分析理论,算法的效率,况且我也没有这个能力.我只是想把一些可以简单的应用到你的C++代码中 的优化技术总结在这里,这样,当你遇到 ... 
- linux下重新定位SVN URL方法
			linux下重新定位SVN URL方法: 如果更换了SVN服务器,就需要重新定位,指向新的SVN URL. 重新定位命令:svn switch --relocate 原svn地址 新svn地址 ... 
