题目链接:戳我

题目大意:给定一个字符串,它的长度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. 仿微信客户端 帧布局中加入fragment

    学习内容来自“慕课网” 这里用Fragment来实现APP主界面 思路: 底部横向排列4个LinearLayout,每个LinearLayout包含一个图片按钮和一个文字 1.默认显示第一个功能(微信 ...

  2. struts框架值栈问题二之值栈的内部结构

    2. 问题二 : 值栈的内部结构 ? * 值栈由两部分组成 > root -- Struts把动作和相关对象压入 ObjectStack 中--List > context -- Stru ...

  3. 数据库日志文件——数据库“xxx”的事务日志已满,原因为“LOG_BACKUP”

    治标的方案: 参考网址:http://blog.csdn.net/tianlianchao1982/article/details/7388616 http://bbs.51cto.com/threa ...

  4. C语言文本处理

    一.conf文本 http://blog.163.com/lixiangqiu_9202/blog/static/53575037201431743236762/ http://blog.csdn.n ...

  5. java jvm入门 jvm什么意思 jvm调优

    接下来我会给大家讲解 国内最顶尖的jvm技术 不服来战

  6. 导出jar文件

    当我们编好一段代码时,就需要将其导出成应用程序,即jar文件(jar文件就是在Java运行环境下运行的应用程序).今天,巩固就教大家用eclipse导出jar文件. 第一步:找到eclipse,双击打 ...

  7. 第八章 连词(Les conjonction )

    ★并列连词(La conjonction de coordination ) ()表示联合关系的并列连词 .et连接肯定的内容.如:    ➞Il conduit vite et bien.      ...

  8. Django模型层(1)

    https://www.cnblogs.com/yuanchenqi/articles/8933283.html MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦, ...

  9. 破解Excel密码

    https://zhidao.baidu.com/question/98055974.html 方法:1\打开文件2\工具---宏----录制新宏---输入名字如:aa3\停止录制(这样得到一个空宏) ...

  10. HDU 1847 Good Luck in CET-4 Everybody! (博弈)

    题意:不用说了吧,都是中文的. 析:虽说这是一个博弈的题,但是也很简单的,在说这个题目前我们先说一下巴什博弈定理. 巴什博弈定理:一堆物品有n个,有两个人(两个人足够聪明)轮流取,规定每次至少取一个, ...