这题说的是给了一串字符 我们要将这个字符 中找出至少出现m次的最长字符串 一个字符课多次使用

利用后缀数组计算最长的lcp

这里有一个点 记得将后缀数组中加入一个空串 如果遇到全部相同的字符时 没办法 判断 倒数第二个和 第三个的大小 因此他们就会被遗漏

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <string.h>
  6. using namespace std;
  7. const int maxn =;
  8. struct SuffixArray
  9. {
  10. int s[maxn];
  11. int sa[maxn];
  12. int t[maxn],t2[maxn],c[maxn];
  13. int rank[maxn],lcp[maxn];
  14. int n;
  15. void build(int m)
  16. {
  17.  
  18. int *x=t,*y=t2,i;
  19. for(i=; i<m; i++ )c[i]=;
  20. for(i=; i<n; i++)c[ x[i]=s[i] ]++;
  21. for(i=; i<m; i++)c[i]+=c[i-];
  22. for(i=n-; i>=; i--)sa[ --c[ x[i] ] ]=i;
  23. for(int k=; k<=n; k<<=)
  24. {
  25. int p=;
  26. for(i=n-k; i<n; i++) y[ p++ ]=i;
  27. for(i=; i<n; i++)if(sa[i]>=k) y[p++ ]= sa[i]-k;
  28. for(i=; i<m; i++) c[ i ]=;
  29. for(i=; i<n; i++) c[ x[ y[i] ] ]++;
  30. for(i=; i<m; i++)c[i]+=c[i-];
  31. for(i=n-; i>=; i--)
  32. sa[ -- c[ x[ y[ i ] ] ] ]=y[i];
  33. for(i=; i<n; i++)rank[ sa[i] ]=i;
  34. p=;
  35. swap(x,y);
  36. x[ sa[] ] =;
  37. for(int i=; i<n; i++)
  38. x[ sa[i] ]= y[ sa[i] ]==y[ sa[i-] ]&&y[ sa[i]+k ]==y[ sa[i-]+k ] ?p-:p++;
  39. if(p>=n)break;
  40. m=p;
  41. }
  42. }
  43. void getLcp()
  44. {
  45. int i,k=;
  46. for(i=; i<n; i++)rank[ sa[ i ] ] =i;
  47. for(i=; i<n; i++)
  48. {
  49. if(k)k--;
  50. int j=sa[ rank[i]- ];
  51. while(s[j+k]==s[i+k])k++;
  52. lcp[ rank[i] ]=k;
  53. }
  54. }
  55. }sa;
  56. bool solve(int len,int m)
  57. {
  58. int L=;
  59. for(int R=; R<=sa.n; R++)
  60. {
  61. if(sa.lcp[R]<len||R==sa.n)
  62. {
  63. if( R-L >= m )return true;
  64. L=R;
  65. }
  66.  
  67. }
  68. return false;
  69. }
  70. int look(int L, int R)
  71. {
  72. int ans=-;
  73. for(int i=L; i<R; i++)
  74. {
  75. ans=max(sa.sa[i],ans);
  76. }
  77. return ans;
  78. }
  79.  
  80. int select(int len, int m)
  81. {
  82. int L=,ad=-;
  83.  
  84. for(int R=; R<=sa.n; R++)
  85. {
  86. if(sa.lcp[R]<len||R==sa.n)
  87. {
  88. if(R-L>=m)
  89. {
  90. ad=max(look(L,R),ad);
  91. }
  92.  
  93. L=R;
  94. }
  95.  
  96. }
  97. return ad;
  98. }
  99. char ss[maxn];
  100. int main()
  101. {
  102. int m;
  103. while(scanf("%d",&m)==)
  104. {
  105.  
  106. if(m==)break;
  107. scanf("%s",ss);
  108.  
  109. sa.n=strlen(ss);
  110. for(int i=; i<sa.n; i++)
  111. {
  112. sa.s[i]= ss[i]-'a'+;
  113. }
  114. if(m==)
  115. {
  116. printf("%d %d\n",sa.n,); continue;
  117. }
  118. sa.s[sa.n]=;
  119. sa.n++;
  120. sa.build();
  121. sa.getLcp();
  122.  
  123. if(solve(,m)==false)
  124. {
  125. puts("none");continue;
  126. }
  127. int L=,R=sa.n,ans;
  128. while(L<=R)
  129. {
  130. int mid=(R+L)/;
  131. if(solve(mid,m)){
  132. ans=mid; L=mid+;
  133. }else R=mid-;
  134. }
  135. printf("%d %d\n",ans,select(ans,m));
  136. }
  137. return ;
  138. }

uva12206 后缀数组的更多相关文章

  1. Uva12206 Stammering Aliens 后缀数组&&Hash

    Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all effort ...

  2. 后缀数组的倍增算法(Prefix Doubling)

    后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...

  3. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  4. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  5. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  6. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  7. 后缀数组(suffix array)详解

    写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具. 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料. 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, ...

  8. 【UOJ #35】后缀排序 后缀数组模板

    http://uoj.ac/problem/35 以前做后缀数组的题直接粘模板...现在重新写一下模板 注意用来基数排序的数组一定要开到N. #include<cstdio> #inclu ...

  9. 【BZOJ-2119】股市的预测 后缀数组

    2119: 股市的预测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 334  Solved: 154[Submit][Status][Discuss ...

随机推荐

  1. ubuntu下文件压缩/解压缩

    ubuntu下文件压缩/解压缩 http://blog.csdn.net/luo86106/article/details/6946255 .gz 解压1:gunzip FileName.gz 解压2 ...

  2. host.conf 文件

    /etc/host.conf文件的作用是设置名称解析时的先后顺序/etc/hosts文件是在使用host解析时,手动的添加的主机记录/etc/relov.conf文件中设置DNS服务器名称以及缺省的域 ...

  3. 获取页面所有链接的JS

    写了一个实用的JS脚本,获取当前页面所有的JS: var str = " \n"; var list = document.getElementsByTagName("a ...

  4. sublime设置tab键为4个空格

    在使用sublime的时候,有时候新建的文件,默认的缩进是2个,那么如何将sublime设置tab键为4个空格呢? 具体方法: 配置: , "translate_tabs_to_spaces ...

  5. 安装coreseek与sphinx遇见的问题

    1.问题 using config file 'etc/csft.conf'...indexing index 'xml'...WARNING: source 'xml': xmlpipe2 supp ...

  6. hadoop完全分布式安装部署-笔记

    规划: [hadoop@db01 ~]$ cat /etc/hosts127.0.0.1   localhost localhost.localdomain localhost4 localhost4 ...

  7. jdb--gdb---java 远程调试(java application与web application)

    命令比较     gdb              jdb     bt             where     del           clear     stop         brea ...

  8. numpy、pandas、scipy介绍

    https://blog.csdn.net/LOLITA0164/article/details/80195124 numpy简介NumPy(Numeric Python)是一个Python包.它是一 ...

  9. HTML5 Storage(永久存储)

    localStorage.aa="aa"; //存储了一个key为aa并且value为aa的键值对: localStorage.setItem("bb", &q ...

  10. Constructing Roads----poj2421(最小生成树Kruskal)

    题目链接: http://poj.org/problem?id=2421 想把n个村庄连接在一起:求最小生成树,不同的是已经有了m条线段链接在一起了,求剩下的: 感觉用Kruskal会简单一点 #in ...