地址:

题目:

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. Asp.net - Razor - 将Model中变量的值赋值给Javascript变量

    <script type="text/javascript"> @{var _userID = Model.UserId.HasValue ? Model.UserId ...

  2. Administrator privileges required for OLE Remote Procedure Call debugging: this feature will not work..

    VC++ 6.0单步调试(F10)出现警告框: 解决方法: 右键VC++ 6.0程序图标

  3. WPF 渲染级别 (Tier)

    在WPF中,显卡的功能相差很大.当WPF评估显卡时,它会考虑许多因素,包括显卡上的RAM数量.对像素着色器(piexl shader)的支持(计算每个像素效果的内置程序,如透明效果),以及对顶点着色器 ...

  4. 让iOS应用支持不同版本的系统与设备

    本文转载至  http://blog.csdn.net/pucker/article/details/11980811 最近一直在做app的iOS 6和7的同时适配工作,所以在此介绍一下系统与设备的兼 ...

  5. js 跨域 之 修改服务器配置-XAMPP-Apache (nginx 拉到最后!)

    js高程第21章提到了ajax 跨域技术,方法有很多,如图: 我主要讲这个: 其实代码就是这样就好了,当然只兼容 IE9 及之后的版本 ,IE9 之前的版本请去原书看吧,Page 600 var xh ...

  6. PAT 甲级 1026 Table Tennis(模拟)

    1026. Table Tennis (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A table ...

  7. Tomcat----->软件密码学基础配置tomcat http连接器 https

    公钥只能私钥解开,私钥只能公钥解开. 类似于别人给你一个盒子,你用他的盒子和自己盒子加密,他手中有他的钥匙和自己的钥匙,可以解开就既能证明是你发的也能相信内容. 每个数据有自己的数据指纹,数据指纹是由 ...

  8. GROUP_CONCAT 拼接顺序

    用 group_concat 拼接后的顺序 group_concat(id order by id) 里面id更新需要更换

  9. python学习笔记(二)— 列表(list)

    列表也叫数组,列表定义,使用[]即可:列表里面可以再套列表,一个里面套一个列表,叫二维数组:一个里面套一个列表,里面的列表再套一个列表,这个叫三维数组,套几层就是几维,定义格式如下: list1 = ...

  10. logback.xml解读----日志配置解读

    初次接触javaweb项目的日志是log4j文件,但是后来发现通过配置logback.xml文件实现日志输出非常好用.经过上午的学习,现总结如下: 直接上配置文件和注释: <?xml versi ...