题目链接:戳我

题目大意:给定一个字符串,它的长度n<=2e5。求长度1~n的子串出现的最大次数。

对于一个子串,它的出现次数是多少?就是它所在endpos集合的大小qwq(注意,这里的大小不指该endpos里面元素的个数,而是指该endpos类在原串中出现的次数为大小)

那么我们怎么累加endpos的大小呢qwq

对于一个np节点(非分裂节点),初始的siz值为1,但是分裂节点(nq)就是0了。(指叶子节点)

然后我们在parent tree上用拓扑排序从下到上累加(因为每个节点的parent是它的子节点的并集)即可。但是因为每个等价类的longest是递增的,所以我们也完全可以开桶,来根据longest的长度进行排序。

之后对于每一个等价类,我们把答案累加到它的longest上面即可,这样的话后面还可以通过小的继承大的来获得等价类中较短字符串的出现次数qwq

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 2500100
using namespace std;
int n,last=1,tot=1;
int siz[MAXN],c[MAXN],a[MAXN],ans[MAXN],ru[MAXN];
char ch[MAXN];
struct Node{int son[26],ff,len;}t[MAXN<<1];
inline void extend(int c)
{
int p=last,np=++tot;last=np;
t[np].len=t[p].len+1;
while(p&&!t[p].son[c]) t[p].son[c]=np,p=t[p].ff;
if(!p) t[np].ff=1;
else
{
int q=t[p].son[c];
if(t[q].len==t[p].len+1) t[np].ff=q;
else
{
int nq=++tot;
t[nq]=t[q],t[nq].len=t[p].len+1;
t[np].ff=t[q].ff=nq;
while(p&&t[p].son[c]==q) t[p].son[c]=nq,p=t[p].ff;
}
}
siz[np]=1;
}
inline void solve()
{
for(int i=1;i<=tot;i++) c[t[i].len]++;
for(int i=1;i<=tot;i++) c[i]+=c[i-1];
for(int i=1;i<=tot;i++) a[c[t[i].len]--]=i;
for(int i=tot;i>=1;i--)
{
int cur=a[i];
siz[t[cur].ff]+=siz[cur];
ans[t[cur].len]=max(ans[t[cur].len],siz[cur]);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%s",ch+1);
int l=strlen(ch+1);
for(int i=1;i<=l;++i) extend(ch[i]-'a');
solve();
for(int i=l-1;i;--i) ans[i]=max(ans[i],ans[i+1]);
for(int i=1;i<=l;++i) printf("%d\n",ans[i]);
return 0;
}

SPOJ Substrings的更多相关文章

  1. 2018.12.15 spoj Substrings(后缀自动机)

    传送门 后缀自动机基础题. 求长度为iii的子串出现次数的最大值. 对原串建出samsamsam,然后用sizsizsiz更新每个maxlenmaxlenmaxlen的答案. 然后由于后缀链接将其转化 ...

  2. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转

    694. Distinct Substrings Problem code: DISUBSTR   Given a string, we need to find the total number o ...

  3. spoj 8222 Substrings (后缀自动机)

    spoj 8222 Substrings 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) 解题思路:我们构造S的SAM,那么对于 ...

  4. 【SPOJ】Substrings(后缀自动机)

    [SPOJ]Substrings(后缀自动机) 题面 Vjudge 题意:给定一个长度为\(len\)的串,求出长度为1~len的子串中,出现最多的出现了多少次 题解 出现次数很好处理,就是\(rig ...

  5. 【SPOJ】Distinct Substrings(后缀自动机)

    [SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...

  6. 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)

    [SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...

  7. SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  8. SPOJ 694 Distinct Substrings

    Distinct Substrings Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. O ...

  9. 【SPOJ】Distinct Substrings

    [SPOJ]Distinct Substrings 求不同子串数量 统计每个点有效的字符串数量(第一次出现的) \(\sum\limits_{now=1}^{nod}now.longest-paren ...

随机推荐

  1. JTemplate学习(一)

    使用模板绑定数据,可以嵌套循环 参考:http://www.doc88.com/p-6621237324128.html <!DOCTYPE html PUBLIC "-//W3C// ...

  2. DNA拷贝数变异CNV检测——基础概念篇

    DNA拷贝数变异CNV检测——基础概念篇   一.CNV 简介 拷贝数异常(copy number variations, CNVs)是属于基因组结构变异(structural variation), ...

  3. Maven系列(一)plugin

    Maven系列(一)plugin maven-compiler-plugin 使用 mvn compile 命令,出现错误: 编码 GBK 的不可映射字符而不能编译.这是因为代码或注释中存在中文引起的 ...

  4. 【附源文件】日记类App原型制作分享-Grid Diary

    Grid Diary是一款非常受文艺青年喜爱的记录应用,它设计简单,内容却非常丰富.它不再是单调的文字记录,界面的设计非常与众不同,由许多格子拼凑而成,每一个格子里面还带有一个问题,十分有趣.提到格子 ...

  5. aspx导出文件

    System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw ...

  6. NAND FLASH和LCD电路图

  7. pyspider示例代码六:传递参数

    传递参数 示例一 #!/usr/bin/env python # -*- encoding: utf- -*- # vim: ts= sts= ff=unix fenc=utf8: # Created ...

  8. Mybatis Blob和String互转,实现文件上传等。

    这样的代码网上有很多,但是本人亲测有bug, 下面是我写的代码.望参考 @MappedJdbcTypes(JdbcType.BLOB) public class BlobAndStringTypeHa ...

  9. Java 关键字有哪些

    数据类型: Boolean(布尔型)    int    long    short    byte    float    double    char    class    interface( ...

  10. java读取网页图片路径并下载到本地

    java读取网页图片路径并下载到本地 最近公司需要爬取一些网页上的数据,自己就简单的写了一个demo,其中有一些数据是图片,需要下载下来到本地并且 将图片的路径保存到数据库,示例代码如下: packa ...