SPOJ Substrings
题目链接:戳我
题目大意:给定一个字符串,它的长度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的更多相关文章
- 2018.12.15 spoj Substrings(后缀自动机)
传送门 后缀自动机基础题. 求长度为iii的子串出现次数的最大值. 对原串建出samsamsam,然后用sizsizsiz更新每个maxlenmaxlenmaxlen的答案. 然后由于后缀链接将其转化 ...
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- spoj 8222 Substrings (后缀自动机)
spoj 8222 Substrings 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) 解题思路:我们构造S的SAM,那么对于 ...
- 【SPOJ】Substrings(后缀自动机)
[SPOJ]Substrings(后缀自动机) 题面 Vjudge 题意:给定一个长度为\(len\)的串,求出长度为1~len的子串中,出现最多的出现了多少次 题解 出现次数很好处理,就是\(rig ...
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- SPOJ 694 Distinct Substrings
Distinct Substrings Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. O ...
- 【SPOJ】Distinct Substrings
[SPOJ]Distinct Substrings 求不同子串数量 统计每个点有效的字符串数量(第一次出现的) \(\sum\limits_{now=1}^{nod}now.longest-paren ...
随机推荐
- 类实现Parcelable接口在Intent中传递
类实现了Parcelable接口就可以在Intent中传递 只要在类的定义中实现Parcelable接口 代码 package entity; import android.os.Parcel; ...
- nginx常用配置说明
nginx的主配置(nginx.conf)说明 #worker进程数量 worker_processes 1; #错误日志 error_log logs/error.log; #进程ID文件 pid ...
- DB2中的数据类型
DB2中的数据类型DB2内置数据类型可以分成数值型(numeric).字符串型(character string).图形字符串(graphic string).二进制字符串型(binary strin ...
- AOP不起作用的原因之一
在-servlet.xml配置context:component-scan后,Spring在扫描包时,会将所有带 @Service注解的类都扫描到容器中.而-servlet.xml和applicati ...
- PhpStorm 2017 激活办法
最新版PhpStorm 2017正式版改进了PHP 7支持,改进代码完成功能.PhpStorm 绝对是PHP开发最好的开发工具. phpstorm已经升级到2017.1,原注册码失效,2017.1.2 ...
- Executor(二)ThreadPoolExecutor、ScheduledThreadPoolExecutor 及 Executors 工厂类
Executor(二)ThreadPoolExecutor.ScheduledThreadPoolExecutor 及 Executors 工厂类 Java 中的线程池类有两个,分别是:ThreadP ...
- 对于cnn的理解
对于神经网络就是给他一个网络各个层之见的传导函数, 之所以这里面用卷积来替代普通的放射函数, 就是因为卷积算的快,hadmard 乘机比矩阵乘法的速度快一个次方,可能都不止. 对于高清晰度的图片算矩阵 ...
- xxnet 360浏览器设置
开xxnet全局pac只能代理. 然后选择360浏览器里面使用ie代理设置就行
- bootstrap中让图片自适应不同的分辨率的方法
boostrap中加上这个样式class="img-responsive"图片就可以自适应,手机端同样适用 详细介绍请查看全文:https://cnblogs.com/qianzf ...
- SpringMVC零碎笔记
在web.xml里可以配置webapp的默认首页,格式如下: <welcome-file-list> <welcome-file>index.html</welcome- ...