【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 \] 题意 给出一个字符串 ...
随机推荐
- 定义JQuery插件
http://cavalry800528.iteye.com/blog/1953917 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$( ...
- Android Studio优秀插件汇总
- element-ui 源码学习
https://athena0304.github.io/element-analysis/ 1.模板字符串实现字符串拼接 typeClass() { return `el-alert--${ thi ...
- windows配置meld
meld 官网:http://meldmerge.org/ git配置: git bash: git config --global merge.tool meld ...
- Asp.Net Core + SignalR 实现实时通信
一.搭建项目 1.创建一个ASP.NET Core MVC 项目 2.nuget 下载和安装 MicroSoft.AspNetCore.SignalR vs提示版本冲突 这时我们选择低版本即可 二.S ...
- 迅为4412开发板Linux驱动教程——总线_设备_驱动注冊流程具体解释
视频下载地址: 驱动注冊:http://pan.baidu.com/s/1i34HcDB 设备注冊:http://pan.baidu.com/s/1kTlGkcR 总线_设备_驱动注冊流程具体解释 • ...
- ajax异步请求,session失效处理
后台拦截器代码: // 判断是否是AJAX请求 if (isAjaxRequest(request)) { log.info("AjaxRequest请求"); ActionCon ...
- vim手记
1.normal 模式进入edit模式 i(a,o),进入 command 模式 :,回到normal模式Esc(ctrl+c,ctrl+[)2.help urs_toc 进入帮助文档目录,退出目录: ...
- 利用css3背景位置完成拼图
.duo { width: 623px; height: 417px; margin: 100px auto; background: url(images/bg1.png) left top no- ...
- jmap 查看内存使用直方图
jps -- 查看进程号 jmap -histo pid 查看堆内存中的对象数目.大小统计直方图, 如果带上live则表示先进行一次fullgc 再统计内存使用情况,如下: jmap -hist ...