地址:

题目:

NSUBSTR - Substrings

no tags 

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
 思路:
  先求出每个状态的endpos集合大小,用cnt记录,用cnt[s]去更新f[len[s]],再用f[i+1]去更新f[i].
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. char ss[];
  6. int ans,f[<<];
  7.  
  8. struct SAM
  9. {
  10. static const int MAXN = <<;//大小为字符串长度两倍
  11. static const int LetterSize = ;
  12.  
  13. int tot, last, ch[MAXN][LetterSize], fa[MAXN], len[MAXN];
  14. int sum[MAXN], tp[MAXN], cnt[MAXN]; //sum,tp用于拓扑排序,tp为排序后的数组
  15.  
  16. void init( void)
  17. {
  18. last = tot = ;
  19. len[] = ;
  20. memset(ch,,sizeof ch);
  21. memset(fa,,sizeof fa);
  22. memset(cnt,,sizeof cnt);
  23. }
  24.  
  25. void add( int x)
  26. {
  27. int p = last, np = last = ++tot;
  28. len[np] = len[p] + , cnt[last] = ;
  29. while( p && !ch[p][x]) ch[p][x] = np, p = fa[p];
  30. if( p == )
  31. fa[np] = ;
  32. else
  33. {
  34. int q = ch[p][x];
  35. if( len[q] == len[p] + )
  36. fa[np] = q;
  37. else
  38. {
  39. int nq = ++tot;
  40. memcpy( ch[nq], ch[q], sizeof ch[q]);
  41. len[nq] = len[p] + , fa[nq] = fa[q], fa[q] = fa[np] = nq;
  42. while( p && ch[p][x] == q) ch[p][x] = nq, p = fa[p];
  43. }
  44. }
  45. }
  46.  
  47. void toposort( void)
  48. {
  49. for(int i = ; i <= len[last]; i++) sum[i] = ;
  50. for(int i = ; i <= tot; i++) sum[len[i]]++;
  51. for(int i = ; i <= len[last]; i++) sum[i] += sum[i-];
  52. for(int i = ; i <= tot; i++) tp[sum[len[i]]--] = i;
  53. for(int i = tot; i; i--) cnt[fa[tp[i]]] += cnt[tp[i]];
  54. }
  55. } sam;
  56.  
  57. int main(void)
  58. {
  59. //freopen("in.acm","r",stdin);
  60. sam.init();
  61. scanf("%s",ss);
  62. for(int i=,len=strlen(ss);i<len;i++) sam.add(ss[i]-'a');
  63. sam.toposort();
  64. for(int i=;i<=sam.tot;i++) f[sam.len[i]]=max(f[sam.len[i]],sam.cnt[i]);
  65. for(int i=sam.len[sam.last];i;i--) f[i]=max(f[i],f[i+]);
  66. for(int i=;i<=sam.len[sam.last];i++) printf("%d\n",f[i]);
  67. return ;
  68. }

spoj8222的更多相关文章

  1. 【spoj8222】 Substrings

    http://www.spoj.com/problems/NSUBSTR/ (题目链接) 题意 给出一个字符串S,令${F(x)}$表示S的所有长度为x的子串出现次数的最大值.求${F(1)..... ...

  2. SPOJ8222 NSUBSTR - Substrings

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  3. [SPOJ8222]Substrings

    [SPOJ8222]Substrings 试题描述 You are given a string S which consists of 250000 lowercase latin letters ...

  4. 【spoj8222】Substrings

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  5. SPOJ8222 Substrings( 后缀自动机 + dp )

    题目大意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.F(1)..F(Length(S)) 建出SAM, 然后求出Right, 求Right可以按拓扑序dp..Right ...

  6. Substrings(SPOJ8222) (sam(后缀自动机))

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

  7. SPOJ8222/NSUBSTR:Substrings——题解

    https://www.luogu.org/problemnew/show/SP8222#sub http://www.spoj.com/problems/NSUBSTR/ 翻译来自洛谷. 你得到一个 ...

  8. SPOJ8222 NSUBSTR - Substrings(后缀自动机)

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

  9. SPOJ8222 NSUBSTR - Substrings 后缀自动机_动态规划

    讲起来不是特别好讲.总之,如果 $dp[i+1]>=dp[i]$,故$dp[i]=max(dp[i],dp[i+1])$ Code: #include <cstdio> #inclu ...

随机推荐

  1. WPF 在TextBox失去焦点时检测数据,出错重新获得焦点解决办法

    WPF 在TextBox失去焦点时检测数据,出错重新获得焦点解决办法 在WPF的TextBox的LostFocus事件中直接使用Focus()方法会出现死循环的问题 正确的使用方式有2中方法: 方法一 ...

  2. C# winform 中MessageBox用法大全(附效果图)

    我们在程序中经常会用到MessageBox. MessageBox.Show()共有21中重载方法.现将其常见用法总结如下: 1.MessageBox.Show(“Hello~~~~”); 最简单的, ...

  3. xc_domain_save.c

    /****************************************************************************** * xc_linux_save.c * ...

  4. ASp.Net控件的生命周期

    服务端事件 页面生命周期 描述 Init Initialization 初始化控件树 LoadViewState Unpack ViewState 从ViewState里提取出状态信息 LoadCon ...

  5. Centos查看系统位数方法

    方法一:file /sbin/init 方法二:file /bin/ls 我的显示是32位

  6. 自定义View中的Path

    我们用Path可以画返回图标,可以画搜索图标,也可以画一个圆,DIDI

  7. ios常用第三方库git下载地址

    本文转载至 http://blog.csdn.net/cerastes/article/details/38348599 iOS第三方库下载常用git 1.FMDB https://github.co ...

  8. $("#btn").click(function(){ });只有在页面加载的时候才会有效触发

    例: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title& ...

  9. 管理源代码的工具SVN与GIT

    如何看待源代码 源代码是公司的重要资产 对应软件公司来说,源代码相当于固定资产>人才 所以源代码管理对于公司来说是最重要的事物之一 一.管理源代码的工具 SVN:集中式的源代码管理工具,通常必须 ...

  10. POJ3150—Cellular Automaton(循环矩阵)

    题目链接:http://poj.org/problem?id=3150 题目意思:有n个数围成一个环,现在有一种变换,将所有距离第i(1<=i<=n)个数小于等于d的数加起来,对m取余,现 ...