题目大意

给一个字符串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. iOS 闭包传值 和 代理传值

    let vc = ViewController() let navc = UINavigationController(rootViewController: vc) window = UIWindo ...

  2. django+xadmin在线教育平台(一)

    大家好,此教程为在慕学网的实战教程Python升级3.6 强力Django+杀手级Xadmin打造在线教育平台的学习笔记,不对望指正! 使用Django+Xadmin打造在线教育平台(Python2, ...

  3. 二十四、MySQL ALTER命令

    MySQL ALTER命令 当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 开始本章教程前让我们先创建一张表,表名为:testalter_tbl. root@ho ...

  4. Python中列表的深浅拷贝

    copy_lst = [ ('py对象三要素',), ('== 比较运算符',), ('is 身份运算符',), ('小数据池',), ('列表的浅拷贝',), ('列表的深拷贝',), ] py对象 ...

  5. Neon Lights in Hong Kong【香港霓虹灯】

    Neon Lights in Hong Kong Neon is to Hong Kong as red phone booths are to London and fog is to San Fr ...

  6. 文件的特殊权限(SUID,SGID,SBIT)

    文件的一般权限:r w x  对应 421  文件的特殊权限:SUID SGID SBIT对应 421  文件的隐藏权限:chattr设置隐藏权限,lsattr查看文件的隐藏权限. 文件访问控制列表: ...

  7. windows下的命令

    1.cd 现在默认只能在当前盘符中改变目录,如果要改变盘符则需要多加一个/d命令. cd /d d:/git/springTest 2.chdir 显示当前目录名或改变当前目录. CHDIR [/D] ...

  8. 【Median of Two Sorted Arrays】cpp

    题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...

  9. Eclipse启动错误:A Java Runtime Environment(JRE) or Java Development Kit(JDK) must be available……

    确保Jdk,Jre都安装完成并且环境变量配置无误的情况下,自动Ecplise报错如下: A Java Runtime Environment (JRE) or Java Development Kit ...

  10. Python 字节与字符串的转换

    html = urlopen("http://www.cnblogs.com/ryanzheng/p/9665224.html") bsObj = BeautifulSoup(ht ...