题目大意

给一个字符串S

令F(x)表示S的所有长度为x的子串中

出现次数的最大值。

求F(1)..F(Length(S))

分析

一个节点\(x\)的长度有\(~~(max(fa),max(x)]\)

出现次数为\(|Right(x)|\)

则\((max(fa),max(x)]\)的出现次数都\(\ge |Right(x)|\)

做法

注意到对于一个点\(x\)的祖先链,长度是[1..max(fa)]

而且他们的\(|Right()|\)都\(\ge |Right(x)|\)

所有更新时对于\(x\)我们直接更新\([1...max(x)]是可以的\)

只更新到\(max(x)\),像后缀和那样求答案就好了

solution

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int M=524288; int go[M][26];
int fa[M];
int stp[M];
int sum[M];
int pos[M];
int right[M];
int last,tot;
char s[M];
int mx[M];
int n; int newnode(int ss){
stp[++tot]=ss;
return tot;
} int ext(int p,int q,int d){
int nq=newnode(stp[p]+1);
fa[nq]=fa[q];
fa[q]=nq;
memcpy(go[nq],go[q],sizeof(go[q]));
for(;p&&go[p][d]==q;p=fa[p]) go[p][d]=nq;
return nq;
} int sam(int p,int d){
int np=go[p][d];
if(np) return (stp[p]+1==stp[np]) ? np : ext(p,np,d);
else{
np=newnode(stp[p]+1);
right[np]=1;
for(;p&&!go[p][d];p=fa[p]) go[p][d]=np;
if(!p) fa[np]=1;
else{
int q=go[p][d];
fa[np]= (stp[p]+1==stp[q]) ? q : ext(p,q,d);
}
}
return np;
} int main(){ int i; scanf("%s",s+1);
n=strlen(s+1); last=tot=1;
for(i=1;i<=n;i++) last=sam(last,s[i]-'a');
for(i=1;i<=tot;i++) sum[stp[i]]++;
for(i=1;i<=n;i++) sum[i]+=sum[i-1];
for(i=1;i<=tot;i++) pos[sum[stp[i]]--]=i;
for(i=tot;i>0;i--) right[fa[pos[i]]]+=right[pos[i]];
for(i=1;i<=tot;i++) mx[stp[i]]=max(mx[stp[i]],right[i]);
for(i=n;i>0;i--) mx[i]=max(mx[i],mx[i+1]);
for(i=1;i<=n;i++) printf("%d\n",mx[i]); return 0;
}

spoj 8222 NSUBSTR 求长度为x的子串中出现次数最大值 SAM的更多相关文章

  1. SPOJ 8222 NSUBSTR(SAM)

    这几天看了N多论文研究了下后缀自己主动机.刚開始蛋疼的看着极短的代码和clj的论文硬是看不懂,后来结合其它几篇论文研究了下.总算是明确了一些 推荐文章http://blog.sina.com.cn/s ...

  2. LCS模板,求长度,并记录子串

    //LCS模板,求长度,并记录子串  //亦可使用注释掉的那些代码,但所用空间会变大 #include<iostream> #include<cstring> #include ...

  3. ●SPOJ 8222 NSUBSTR - Substrings(后缀数组)

    题链: http://www.spoj.com/problems/NSUBSTR/ 题解: 同届红太阳 --WSY给出的后缀数组解法!!! 首先用倍增算法求出 sa[i],rak[i],hei[i]然 ...

  4. SPOJ 8222 NSUBSTR - Substrings

    http://www.spoj.com/problems/NSUBSTR/ 题意: F(x)定义为字符串S中所有长度为x的子串重复出现的最大次数 输出F[1]~F[len(S)] 用字符串S构建后缀自 ...

  5. 【刷题】SPOJ 8222 NSUBSTR - Substrings

    You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as ...

  6. 【uva11855-求长度为1到n的相同子串出现的次数】sam

    题意:求长度为1到n的相同子串出现的次数,输到小于2为止. 题解: 用sam做. 建机,算right集合,然后用r[i]更新长度为step[i]的子串出现次数,然后ans[i]=maxx(ans[i] ...

  7. ●SPOJ 8222 NSUBSTR–Substrings

    题链: http://www.spoj.com/problems/NSUBSTR/题解: 后缀自动机. 不难发现,对于自动机里面的一个状态s, 如果其允许的最大长度为maxs[s],其right集合的 ...

  8. ●SPOJ 8222 NSUBSTR–Substrings(后缀自动机)

    题链: http://www.spoj.com/problems/NSUBSTR/ 题解: 后缀自动机的水好深啊!懂不了相关证明,带着结论把这个题做了.看来这滩深水要以后再来了. 本题要用到一个叫 R ...

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

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

随机推荐

  1. JZOJ 4269. 【NOIP2015模拟10.27】挑竹签

    4269. [NOIP2015模拟10.27]挑竹签 (File IO): input:mikado.in output:mikado.out Time Limits: 1000 ms  Memory ...

  2. requests.exceptions.SSLError……Max retries exceeded with url错误求助!!!

    import requests head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Appl ...

  3. C++ vector 实例

    #include <string> #include <iostream> #include <vector> #include <algorithm> ...

  4. Android 自定义debug.keystore

    场景分析: 有时候,我们要使用第三方的服务,需要提供自己的包名以及keystore的sha1值,比如微信支付,百度地图,都需要包名和keystore的sha1值作为唯一标识.这时候我们测试的时候,如果 ...

  5. 连续小波变换(CWT)

    整理下时频分析变换的方法,遇见好的文章就记录下来了,本篇博客参考知乎https://www.zhihu.com/topic/19621077/top-answers上的一个回答,自己手敲一遍,增强记忆 ...

  6. MySQL基础2-创建表和主键约束

    1.创建表 在操作数据表之前,应该使用"USE 数据库名"指定操作是在哪个数据库中进行 主键约束(唯一标识) ****非空*** ****唯一*** ****被引用****(学习外 ...

  7. React-表单操作

    用户在表单填入的内容,属于用户跟组件的互动,所以不能用 this.props 读取 <!DOCTYPE html> <html lang="zh-cn"> ...

  8. Python框架之Django学习笔记(一)

    Django历史: Django 是从真实世界的应用中成长起来的,它是由 堪萨斯(Kansas)州 Lawrence 城中的一个 网络开发小组编写的. 它诞生于 2003 年秋天,那时 Lawrenc ...

  9. 基于web自动化测试框架的设计与开发(讲解演示PPT)

  10. LeetCode with Python -> String

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...