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(后缀自动机))的更多相关文章

  1. Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))

    Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...

  2. Lexicographical Substring Search (spoj7259) (sam(后缀自动机)+第k小子串)

    Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...

  3. 弦论(tjoi2015,bzoj3998)(sam(后缀自动机))

    对于一个给定长度为\(N\)的字符串,求它的第\(K\)小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串\(S\) 第二行为两个整数\(T\)和\(K\),\(T\)为0则表示不同 ...

  4. 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, \(\ ...

  5. LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

  6. sam(后缀自动机)

    后缀自动机ins解释 void ins(int c){ int p=last;//将当前节点的parent节点变为last int np=++cnt;//建立新节点 last=np;//将last设为 ...

  7. Luogu P3346 [ZJOI2015]诸神眷顾的幻想乡 广义SAM 后缀自动机

    题目链接 \(Click\) \(Here\) 真的是好题啊-不过在说做法之前先强调几个自己总是掉的坑点. 更新节点永远记不住往上跳\(p = fa[p]\) 新建节点永远记不住\(len[y] = ...

  8. 字符串(tjoi2016,heoi2016,bzoj4556)(sam(后缀自动机)+线段树合并+倍增+二分答案)

    佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了 一个长为\(n\)的字符串\(s\),和\(m\)个问题.佳媛姐姐必须正确回答这\(m\)个问题, ...

  9. 后缀自动机SAM学习笔记

    前言(2019.1.6) 已经是二周目了呢... 之前还是有一些东西没有理解到位 重新写一下吧 后缀自动机的一些基本概念 参考资料和例子 from hihocoder DZYO神仙翻译的神仙论文 简而 ...

随机推荐

  1. 记一次 .net core publish 之后找不到视图文件的问题

    周末将VS从15.3 升级到15.5 ,之后又将VS版本由社区版,升级为企业版. 于是问题来了: 项目publish 之后找不到视图文件了??? 问题很是蛋疼,后来仔细想了一下,也没有动什么东西.查看 ...

  2. HDFS Architecture Notes

    [HDFS Architecture Notes] 1.Moving Computation is Cheaper than Moving Data A computation requested b ...

  3. R_CNN

    https://blog.csdn.net/briblue/article/details/82012575 背景本篇论文的题目是 <Rich feature hierarchies for a ...

  4. UNITY5 - fog

    全局雾效在 window->lighting 的设置,可设置颜色和距离 注意摄像机的Rendering Path要选择Forward

  5. 利率计算v2.0--web版--软件工程

    .客户说:帮我开发一个复利计算软件. .如果按照单利计算,本息又是多少呢? .假如30年之后要筹措到300万元的养老金,平均的年回报率是3%,那么,现在必须投入的本金是多少呢? .利率这么低,复利计算 ...

  6. code1001 舒适的路线

    n次最小生成树kruskal 将所有的边排序,权值小的在前. 设排序后第i条边为路径中的最长边,那么这条路径一定是由1~i中的一些边组成 因为最高速和最低速的差尽量小,最高速确定了,最低速应尽量大. ...

  7. cakephp获取最后一条sql语句

    .在app\config\core.php中设置Configure::write(); .页面上追加如下代码: $dbo = ConnectionManager::getDataSource('def ...

  8. android4.2 高用zing拍照后,返回其它页面操作时,主线程关掉或程序退出的问题解决

    产生错误的代码: @Override protected void onCreate(Bundle savedInstanceState) { StrictMode.setThreadPolicy(n ...

  9. 動態修改 XML 欄位

    /* -- for test DECLARE @content VARCHAR(50) DECLARE @folioId VARCHAR(50) DECLARE @opinionType VARCHA ...

  10. Oracle EBS Request Status: Pending

    如果提交请求以后,状态一直是pending状态,可以在"工具"打开"Manager",查看一下Maximum是否有设置错,另外pending的数量当前是多少. ...