Substrings(SPOJ8222) (sam(后缀自动机))
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
题意:
给定一个字符串\(S\),令\(F(x)\)表示\(S\)中所有长度为\(x\)的子串出现最多的次数,
题解:
把串塞进一个后缀自动机,在\(parent\)树上反向拓扑,求出每个长度时的最大值。
#include<bits/stdc++.h>
using namespace std;
const int N=2000010;
char s[N];
int a[N],c[N],ans[N];
struct SAM{
int last,cnt;
int size[N],ch[N][26],fa[N<<1],l[N<<1];
void ins(int c){
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=1;
else{
int q=ch[p][c];
if(l[p]+1==l[q])fa[np]=q;
else{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
size[np]=1;
}
void build(char s[]){
int len=strlen(s+1);
last=cnt=1;
for(int i=1;i<=len;++i)ins(s[i]-'a');
}
void calc(int len){
for(int i=1;i<=cnt;++i)c[l[i]]++;
for(int i=1;i<=cnt;++i)c[i]+=c[i-1];
for(int i=1;i<=cnt;++i)a[c[l[i]]--]=i;
for(int i=cnt;i;--i){
int p=a[i];
size[fa[p]]+=size[p];
ans[l[p]]=max(ans[l[p]],size[p]);
}
for(int i=1;i<=len;++i){
printf("%d\n",ans[i]);
}
}
}sam;
int main(){
cin>>s+1;
sam.build(s);
sam.calc(strlen(s+1));
}
Substrings(SPOJ8222) (sam(后缀自动机))的更多相关文章
- Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))
Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...
- Lexicographical Substring Search (spoj7259) (sam(后缀自动机)+第k小子串)
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- 弦论(tjoi2015,bzoj3998)(sam(后缀自动机))
对于一个给定长度为\(N\)的字符串,求它的第\(K\)小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串\(S\) 第二行为两个整数\(T\)和\(K\),\(T\)为0则表示不同 ...
- LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
- LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
- sam(后缀自动机)
后缀自动机ins解释 void ins(int c){ int p=last;//将当前节点的parent节点变为last int np=++cnt;//建立新节点 last=np;//将last设为 ...
- Luogu P3346 [ZJOI2015]诸神眷顾的幻想乡 广义SAM 后缀自动机
题目链接 \(Click\) \(Here\) 真的是好题啊-不过在说做法之前先强调几个自己总是掉的坑点. 更新节点永远记不住往上跳\(p = fa[p]\) 新建节点永远记不住\(len[y] = ...
- 字符串(tjoi2016,heoi2016,bzoj4556)(sam(后缀自动机)+线段树合并+倍增+二分答案)
佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了 一个长为\(n\)的字符串\(s\),和\(m\)个问题.佳媛姐姐必须正确回答这\(m\)个问题, ...
- 后缀自动机SAM学习笔记
前言(2019.1.6) 已经是二周目了呢... 之前还是有一些东西没有理解到位 重新写一下吧 后缀自动机的一些基本概念 参考资料和例子 from hihocoder DZYO神仙翻译的神仙论文 简而 ...
随机推荐
- Maven编译并打包Mahout CDH版源码
目录 1. 问题描述 最近在使用Mahout里的推荐算法进行实验,由于业务需求,需要修改Mahout源码,将原本输出到HDFS上的结果输出到HBase中.由于Mahout发布的源码都是Maven项目, ...
- python传值&值引用
[python传值&值引用] 和其他语言不一样,传递参数的时候,python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方式.实际上,这种方式相当于传值 ...
- The Process of Google Hiring
[The Process of Google Hiring] 1.keynote 1: The Google hiring process is designed to hire the most t ...
- 把CString转化为char*
转:http://blog.sina.com.cn/s/blog_58e19ae7010003jt.html 正确方法:CString m_Head:char *codefile;codefile=( ...
- react-native 打包 出apk
先上步骤: 一. 生成签名文件(my-release-key.keystore文件) Android要求所有应用都有一个数字签名才会被允许安装在用户手机上 1. 在项目目录下运行如下命令: keyt ...
- php识别二维码
php-zbarcode 是 PHP 读取条形码的扩展模块,目前仅支持 php5.x
- python 多线程简介
Thread类定义了以下常用方法与属性: Thread.getName() \Thread.setName():老方式用于获取和设置线程的名称,官方建议用Thread.name替代 Thread.id ...
- 友盟统计小白教程:创建应用,申请appkey
上回书讲到,我们已经和一个靠谱的人选择一个靠谱的统计平台注册了一个帐号,下面就该创建一个应用了. 介绍一个基础知识: appkey:友盟识别app的唯一标识,目前友盟平台上超过500000款App,每 ...
- Android ListView的一个坑,你可掉进去过?
需要的功能很简单,就是一个带checkbox的列表,提交时需要知道用户选择了那些项目,如下图: 使用SimpleAdapter作为数据适配器,重写SimpleAdapter.ViewBinder的方法 ...
- 年末福利,C/S应用升级更新完整解决方案放送
程序员,工作累寿命短,大家应该学会分享,别浪费有限的生命与健康做重复的事情. C/S方式实现的应用有个升级更新功能是必需的,以前整过一个但是没考虑多套C/S应用的情况,那个时候公司只有一套系统,现在又 ...