http://www.spoj.com/problems/NSUBSTR/

题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值。求F(1)..F(Length(S))

这题做法:

首先建立字符串的后缀自动机。

所以对于一个节点$s$,长度范围为$[Min(s), Max(s)]$,出现次数是$|Right(s)|$,那么我们用$|Right(s)|$去更新$f(Max(s))$,最后用$f(i)$更新$f(i-1)$即可。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <set>
  9. #include <map>
  10. using namespace std;
  11. typedef long long ll;
  12. #define rep(i, n) for(int i=0; i<(n); ++i)
  13. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  14. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  15. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  16. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  17. #define CC(i,a) memset(i,a,sizeof(i))
  18. #define read(a) a=getint()
  19. #define print(a) printf("%d", a)
  20. #define dbg(x) cout << (#x) << " = " << (x) << endl
  21. #define error(x) (!(x)?puts("error"):0)
  22. #define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
  23. inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
  24.  
  25. struct sam {
  26. static const int N=1000005;
  27. int c[N][26], l[N], f[N], root, last, cnt;
  28. sam() { cnt=0; root=last=++cnt; }
  29. void add(int x) {
  30. int now=last, a=++cnt; last=a;
  31. l[a]=l[now]+1;
  32. for(; now && !c[now][x]; now=f[now]) c[now][x]=a;
  33. if(!now) { f[a]=root; return; }
  34. int q=c[now][x];
  35. if(l[q]==l[now]+1) { f[a]=q; return; }
  36. int b=++cnt;
  37. memcpy(c[b], c[q], sizeof c[q]);
  38. l[b]=l[now]+1;
  39. f[b]=f[q];
  40. f[q]=f[a]=b;
  41. for(; now && c[now][x]==q; now=f[now]) c[now][x]=b;
  42. }
  43. void build(char *s) {
  44. int len=strlen(s);
  45. rep(i, len) add(s[i]-'a');
  46. }
  47. }a;
  48.  
  49. const int N=250005;
  50. char s[N];
  51. int f[N], r[1000005], t[N], b[1000005];
  52. void getans(int len) {
  53. int *l=a.l, *p=a.f, cnt=a.cnt;
  54. for(int now=a.root, i=0; i<len; ++i) now=a.c[now][s[i]-'a'], ++r[now];
  55. for1(i, 1, cnt) ++t[l[i]];
  56. for1(i, 1, len) t[i]+=t[i-1];
  57. for1(i, 1, cnt) b[t[l[i]]--]=i;
  58. for3(i, cnt, 1) r[p[b[i]]]+=r[b[i]];
  59. for1(i, 1, cnt) f[l[i]]=max(f[l[i]], r[i]);
  60. for3(i, len, 1) f[i]=max(f[i+1], f[i]);
  61. for1(i, 1, len) printf("%d\n", f[i]);
  62. }
  63. int main() {
  64. scanf("%s", s);
  65. a.build(s);
  66. getans(strlen(s));
  67. return 0;
  68. }

  


You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as the maximal number of times that some string with length x appears in S. For example for string 'ababa' F(3) will be 2 because there is a string 'aba' that occurs twice. Your task is to output F(i) for every i so that 1<=i<=|S|.

Input

String S consists of at most 250000 lowercase latin letters.

Output

Output |S| lines. On the i-th line output F(i).

Example

  1. Input:
    ababa
  2.  
  3. Output:
    3
    2
    2
    1
    1

【SPOJ】8222. Substrings(后缀自动机)的更多相关文章

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

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

  2. SPOJ NSUBSTR Substrings 后缀自动机

    人生第一道后缀自动机,总是值得纪念的嘛.. 后缀自动机学了很久很久,先是看CJL的论文,看懂了很多概念,关于right集,关于pre,关于自动机的术语,关于为什么它是线性的结点,线性的连边.许多铺垫的 ...

  3. SPOJ NSUBSTR Substrings ——后缀自动机

    建后缀自动机 然后统计次数,只需要算出right集合的大小即可, 然后更新f[l[i]]和rit[i]取个max 然后根据rit集合短的一定包含长的的性质,从后往前更新一遍即可 #include &l ...

  4. spoj 1812 lcsII (后缀自动机)

    spoj 1812 lcsII (后缀自动机) 题意:求多个串的lcs,最多10个串,每个串最长10w 解题思路:后缀自动机.先建好第一个串的sam,然后后面的串拿上去跑(这个过程同前一题).sam上 ...

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

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

  6. SPOJ 8222. Substrings(后缀自动机模板)

    后缀自动机+dp. 后缀自动机主要是在functioner大牛那里学习的:http://blog.sina.com.cn/s/blog_70811e1a01014dkz.html 这道题是在No_st ...

  7. Substrings SPOJ - NSUBSTR (后缀自动机)

    Substrings \[ Time Limit: 100ms\quad Memory Limit: 1572864 kB \] 题意 给出一个长度为 \(250000\) 的字符串,求出所有 \(x ...

  8. POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀自动机)

    题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 确实比后缀数组快多了(废话→_→). \(Description\) 求两个字符串最长公共子串 ...

  9. 【CF316G3】Good Substrings 后缀自动机

    [CF316G3]Good Substrings 题意:给出n个限制(p,l,r),我们称一个字符串满足一个限制当且仅当这个字符串在p中的出现次数在[l,r]之间.现在想问你S的所有本质不同的子串中, ...

  10. Lexicographical Substring Search SPOJ - SUBLEX (后缀自动机)

    Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串 ...

随机推荐

  1. react-keeper

    通过 react-keeper 替换掉 react-router 解决 页面缓存问题.

  2. js setTimeout和setInterval区别

    1.区别 2.示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset=&qu ...

  3. jquery remove()不兼容问题解决方案

      jquery remove()不兼容问题解决方案 CreationTime--2018年7月27日10点19分 Author:Marydon 1.情景展示 点击关闭,将这个div移除掉 源码展示 ...

  4. 基于canvas的仪表盘效果

    概述 基于Canvas实现的仪表盘及效果.通过配置参数,可以任意修改仪表盘颜色,刻度,动画过渡时间等,满足不同场景下的使用.同时使用原生的Canvas,也是学习Canvas的很好的例子. 详细 代码下 ...

  5. Echarts实例

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. ECMAScript和JavaScript的区别,ECMAScript发展更新历史,ECMAScript5和ECMAScript6的新特性及浏览器支持情况,ECMAScript 5/ECMAScript 2015正式发布

    ECMAScript和JavaScript的区别 ECMA是European Computer Manufacturers Association的缩写,即欧洲计算机制造商协会.欧洲计算机制造商协会是 ...

  7. 完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载

    在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符. []运算符重载 +运算符重载 +=运算符重载 <<运算符重载 >>运算符重 ...

  8. 使用原生SQL返回实体类具体实现详情

    注:可以直接复制粘贴,欢迎提出各种问题,谢谢! 因为网上查询大都是相同的,自己做时发现很多不懂,摸索了很久才弄懂,所以写了这个例子,比较容易看懂吧. 使用原生SQL查询并将结果返回实体中: (1)因为 ...

  9. laravel路由之分组路由

    laravel下的分组路由可以嵌套如下: Route::group(['prefix'=>'admin'],function(){ Route::group(['prefix'=>'dtk ...

  10. centos 无法ping内网 Destination Host Unreachable

    centos 突然无法ping内网了. 本来是一直是好好的. 在这之前,当前服务器(centos 192.168.1.30)大量的在操作内网192.168.1.20服务器的数据库.. 会不会是流量大了 ...