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 ...
随机推荐
- 7-找了一上午的BUG
#include <iostream>#include <cstring>#include <algorithm>#define MAX 1<<28;u ...
- 【JAVA】通过HttpURLConnection 上传和下载文件(二)
HttpURLConnection文件上传 HttpURLConnection采用模拟浏览器上传的数据格式,上传给服务器 上传代码如下: package com.util; import java.i ...
- Spring Boot之初始化项目
最简单的从Spring官网下载 :地址 https://start.spring.io/ 下拉选择需要的Spring Boot版本 修改Group和Artifact 直接点击Generate Pro ...
- 前端之JavaScript笔记3
一 创建添加节点 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- hdu-1728(bfs+优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728 注意:1.先输入起点(y1,x1)和终点(y2,x2): 2.如果一个一个遍历会超时. 思路:每 ...
- [转]关于docker包存储结构说明
原文:http://blog.csdn.net/w412692660/article/details/49005631 前段时间与同事交流docker的安装包层次结构,并沟通相关每个文件的作用,但是一 ...
- java报错java/lang/NoClassDefFoundError: java/lang/Object
安装完java出错 javac和java -version 都无效,报错如上 解决方法,更改文件中的两个文件(前提是你的 vim /etc/profile 文件路径写的正确) /usr/java/ ...
- Oracle零碎总结:结构-工具-创建语句
前言:Oracle内部的存储及管理结构是1.数据库系统:2.数据库实例:3.表空间,系统用户system,普通用户:表,视图,触发器,存储过程等: 一.Oracle数据库系统和数据库实例的对应关系是一 ...
- struts2 action 中autowired 不能注入
一.pom.xml <dependency> <groupId>org.apache.struts</groupId> <artifactId>stru ...
- 百度离线地图,web
1.首先获取百度 JavaScript API 首先用浏览器打开 http://api.map.baidu.com/api?v=1.3 其中 http://api.map.baidu.com/gets ...