求重复k次的最长重复子串,解法见罗穗骞大神的后缀数组论文

  1. const maxn=;
  2.  
  3. var
  4. x,y,rank,sa,h,s,num,c:array[..maxn] of longint;
  5. n,time:longint;
  6.  
  7. function max(x,y:longint):longint; begin if x>y then exit(x) else exit(y); end;
  8. function min(x,y:longint):longint; begin if x<y then exit(x) else exit(y); end;
  9. procedure make;
  10. var i,j,p,tot:longint;
  11. begin
  12. p:=;
  13. while p<n do
  14. begin
  15. fillchar(c,sizeof(c),);
  16. for i:= to n-p do y[i]:=rank[i+p];
  17. for i:= n-p+ to n do y[i]:=;
  18. for i:= to n do inc(c[y[i]]);
  19. for i:= to n do inc(c[i],c[i-]);
  20. for i:= to n do
  21. begin
  22. sa[c[y[i]]]:=i;
  23. dec(c[y[i]]);
  24. end;
  25. fillchar(c,sizeof(c),);
  26. for i:= to n do x[i]:=rank[i];
  27. for i:= to n do inc(c[x[i]]);
  28. for i:= to n do inc(c[i],c[i-]);
  29. for i:= n downto do
  30. begin
  31. y[sa[i]]:=c[x[sa[i]]];
  32. dec(c[x[sa[i]]]);
  33. end;
  34. for i:= to n do sa[y[i]]:=i;
  35. tot:=;
  36. rank[sa[]]:=;
  37. for i:= to n do
  38. begin
  39. if (x[sa[i]]<>x[sa[i-]]) or (x[sa[i]+p]<>x[sa[i-]+p]) then inc(tot);
  40. rank[sa[i]]:=tot;
  41. end;
  42. p:=p<<;
  43. end;
  44. end;
  45.  
  46. procedure makeht;
  47. var i,j,p:longint;
  48. begin
  49. h[]:=; p:=;
  50. for i:= to n do
  51. begin
  52. p:=max(p-,);
  53. if rank[i]= then continue;
  54. j:=sa[rank[i]-];
  55. while (i+p<=n) and (j+p<=n) and (s[i+p]=s[j+p]) do inc(p);
  56. h[rank[i]]:=p;
  57. end;
  58. end;
  59.  
  60. procedure init;
  61. var i,j,tot:longint;
  62. ch:char;
  63. begin
  64. readln(n,time);
  65. for i:= to n do readln(s[i]);
  66. for i:= to n do x[i]:=s[i];
  67. fillchar(c,sizeof(c),);
  68. for i:= to n do inc(c[x[i]]);
  69. for i:= to do inc(c[i],c[i-]);
  70. for i:= to n do
  71. begin
  72. sa[c[x[i]]]:=i;
  73. dec(c[x[i]]);
  74. end;
  75. rank[sa[]]:=;
  76. tot:=;
  77. for i:= to n do
  78. begin
  79. if x[sa[i]]<>x[sa[i-]] then inc(tot);
  80. rank[sa[i]]:=tot;
  81. end;
  82. make;
  83. makeht;
  84. end;
  85.  
  86. function check(k:longint):boolean;
  87. var i,j,x,y:longint;
  88. begin
  89. i:=;
  90. while i<=n do
  91. begin
  92. j:=i;
  93. while (j<n) and (h[j+]>=k) do inc(j);
  94. if j-i+>=time then exit(true);
  95. i:=j+;
  96. end;
  97. exit(false);
  98. end;
  99.  
  100. procedure solve;
  101. var l,r,mid,ans:longint;
  102. begin
  103. l:=; r:=n>>; ans:=;
  104. while l<=r do
  105. begin
  106. mid:=(l+r)>>;
  107. if check(mid) then
  108. begin
  109. l:=mid+;
  110. ans:=mid;
  111. end
  112. else r:=mid-;
  113. end;
  114. writeln(ans);
  115. end;
  116.  
  117. Begin
  118. init;
  119. solve;
  120. End.

【BZOJ1717】[Usaco2006 Dec]Milk Patterns 产奶的模式 (二分+SA)的更多相关文章

  1. bzoj1717: [Usaco2006 Dec]Milk Patterns 产奶的模式

    后缀数组+二分答案+离散化.(上次写的时候看数据小没离散化然后一直WA...写了lsj师兄的写法. #include<cstdio> #include<cstring> #in ...

  2. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式( 二分答案 + 后缀数组 )

    二分答案m, 后缀数组求出height数组后分组来判断. ------------------------------------------------------------ #include&l ...

  3. [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式_后缀数组_二分答案

    Milk Patterns 产奶的模式 bzoj-1717 Usaco-2006 Dec 题目大意:给定一个字符串,求最长的至少出现了$k$次的子串长度. 注释:$1\le n\le 2\cdot 1 ...

  4. bzoj1717: [Usaco2006 Dec]Milk Patterns 产奶的模式(后缀数组+二分)

    /* 求可重叠的至少重复K次的最长字串 以1为下标起点,因为a[i]最大到1000000,所以要先离散一下 二分长度len 然后O(n)检验 后看h[i]是否有连续的一段h[i]大于len的,并且h[ ...

  5. [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式 (hash构造后缀数组,二分答案)

    以后似乎终于不用去学后缀数组的倍增搞法||DC3等blablaSXBK的方法了= = 定义(来自关于后缀数组的那篇国家集训队论文..) 后缀数组:后缀数组SA是一个一维数组,它保存1..n的某个排列S ...

  6. [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式——后缀数组

    Brief Description 给定一个字符串,求至少出现k次的最长重复子串. Algorithm Design 先二分答案,然后将后缀分成若干组.判断有没有一个组的后缀个数不小于k.如果有,那么 ...

  7. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: ...

  8. BZOJ#1717:[Usaco2006 Dec]Milk Patterns 产奶的模式(后缀数组+单调队列)

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的 ...

  9. BZOJ_1717_[Usaco2006 Dec]Milk Patterns 产奶的模式_后缀数组

    BZOJ_1717_[Usaco2006 Dec]Milk Patterns 产奶的模式_后缀数组 Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他 ...

随机推荐

  1. 【CF739E】Gosha is hunting(WQS二分套WQS二分)

    点此看题面 大致题意: 你有两种捕捉球(分别为\(A\)个和\(B\)个),要捕捉\(n\)个神奇宝贝,第\(i\)个神奇宝贝被第一种球捕捉的概率是\(s1_i\),被第二种球捕捉的概率是\(s2_i ...

  2. MySQL8.0在Windows下的安装和使用

    前言 MySQL在Windows下有2种安装方式:1.图形化界面方式安装MySQL 2.noinstall方式安装MySQL.在这里,本文只介绍第二种方式:以noinstall方式安装MySQL,以及 ...

  3. Finite Encyclopedia of Integer Sequences(找规律)

    6617: Finite Encyclopedia of Integer Sequences 时间限制: 1 Sec  内存限制: 128 MB提交: 375  解决: 91[提交] [状态] [讨论 ...

  4. angular6项目中使用echarts图表的方法(有一个坑,引用报错)

    1.安装相关依赖(采用的webpack) npm install ecahrts --save npm install ngx-echarts --save 2.angular.json 配置echa ...

  5. java常用 开源

    http://sourceforge.nethttp://code.google.com/hosting/http://www.open-open.com/code/tags/Javahttp://w ...

  6. c++ 作业 10月13日 进制转换最简单方法,控制c++输出格式方法 教材50的表格自己实践一下 例题3.1 setfill() setw()

    #include <iostream> #include <iomanip> using namespace std; int main(){ // int i; // cou ...

  7. response.setContentType("text/html;charset=utf-8")后依然乱码的解决方法

    从浏览器获取数据到服务器,服务器将得到数据再显示在浏览器上英文字母正常显示,中文字符乱码的问题,已经使用了 response.setContentType("text/html;charse ...

  8. notify()和notifyAll()主要区别

    notify()和notifyAll()都是Object对象用于通知处在等待该对象的线程的方法. void notify(): 唤醒一个正在等待该对象的线程.void notifyAll(): 唤醒所 ...

  9. JavaScript之map与parseInt的陷阱

    问题来源 ​ 这个问题的来源是学习廖雪峰老师JS教程.问题如下:小明希望利用map()把字符串变成整数,他写的代码很简洁: 'use strict'; var arr = ['1', '2', '3' ...

  10. 【IDEA】热部署插件Jrebel破解安装

    JRebel 介绍 IDEA上原生是不支持热部署的,一般更新了 Java 文件后要手动重启 Tomcat 服务器,才能生效,浪费不少生命啊.目前对于idea热部署最好的解决方案就是安装JRebel插件 ...