题目大意

给一个字符串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. 【贪心 二分图 线段树】cf533A. Berland Miners

    通过霍尔定理转化判定方式的一步还是很妙的 The biggest gold mine in Berland consists of n caves, connected by n - 1 transi ...

  2. Jquery 就是怎么取得一个select的当前值

    <select id="cursel">: <option value="1">值1</option>: <optio ...

  3. 【Python学习之七】面向对象高级编程——使用@property

    参考来自廖雪峰Python教程:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ ...

  4. GNU汇编逻辑或算数左移右移

    lsl 左移 .text .global  _start _start: mov r1,#0b1 mov r1,r1,lsl#2 ROR循环右移 .text .global  _start _star ...

  5. Darwin's Letter【达尔文的信】

    Darwin's Letter A letter written by Charles Darwin in 1875 has been returned to the Smithsonian Inst ...

  6. Codeforces Round #464 (Div. 2) C. Convenient For Everybody

    C. Convenient For Everybody time limit per test2 seconds memory limit per test256 megabytes Problem ...

  7. collections模块简介

    collections模块简介 除python提供的内置数据类型(int.float.str.list.tuple.dict)外,collections模块还提供了其他数据类型,使用如下功能需先导入c ...

  8. VBA连接到SQL2008需要加上端口号

    VBA连接到SQL2008需要加上端口号1433,比如 conn = "server=XXXX.XXXX.XXXX.XXXX,1433;provider=SQLOLEDB.1;databas ...

  9. Java基础知识回顾(一):字符串小结

    Java的基础知识回顾之字符串 一.引言 很多人喜欢在前面加入赘述,事实上去技术网站找相关的内容的一般都应当已经对相应知识有一定了解,因此我不再过多赘述字符串到底是什么东西,在官网中已经写得很明确了, ...

  10. loj2071 「JSOI2016」最佳团体

    分数规划+树形依赖背包orz #include <iostream> #include <cstring> #include <cstdio> #include & ...