首先后缀数组预处理
然后二分答案len很显然,然后考虑怎么判定
我们用左右指针顺着名次扫描一下,初始左右指针为1
根据LCP(i,j)=min(height[rank[i]+1]~height[rank[j]]) 设rank[i]<rank[j]
移动右指针扫描得到一组后缀[i,j]之间LCP>=len,h[j+1]<len
然后判断一下这组后缀是否有超过半数的原串,如果满足则记录
然后左右指针都从j+1开始
由于每个后缀最多被扫描一次判断一次,所以必然O(n)
注意多个串相连接的时候要用不同的字符作为分隔符

  1. var ans,x,y,sa,sum,rank,h,loc:array[..] of longint;
  2. v:array[..] of boolean;
  3. j,len,l,r,k,i,n,m,p,tot:longint;
  4. s,ch:ansistring;
  5.  
  6. procedure suffix;
  7. begin
  8. fillchar(sum,sizeof(sum),);
  9. for i:= to n do
  10. begin
  11. y[i]:=ord(s[i]);
  12. inc(sum[y[i]]);
  13. end;
  14. m:=;
  15. for i:= to m do
  16. sum[i]:=sum[i-]+sum[i];
  17. for i:=n downto do
  18. begin
  19. sa[sum[y[i]]]:=i;
  20. dec(sum[y[i]]);
  21. end;
  22. p:=;
  23. rank[sa[]]:=;
  24. for i:= to n do
  25. begin
  26. if (y[sa[i]]<>y[sa[i-]]) then inc(p);
  27. rank[sa[i]]:=p;
  28. end;
  29. m:=p;
  30. j:=;
  31. while m<n do
  32. begin
  33. fillchar(sum,sizeof(sum),);
  34. y:=rank;
  35. p:=;
  36. for i:=n-j+ to n do
  37. begin
  38. inc(p);
  39. x[p]:=i;
  40. end;
  41. for i:= to n do
  42. if sa[i]>j then
  43. begin
  44. inc(p);
  45. x[p]:=sa[i]-j;
  46. end;
  47.  
  48. for i:= to n do
  49. begin
  50. rank[i]:=y[x[i]];
  51. inc(sum[rank[i]]);
  52. end;
  53. for i:= to m do
  54. inc(sum[i],sum[i-]);
  55. for i:=n downto do
  56. begin
  57. sa[sum[rank[i]]]:=x[i];
  58. dec(sum[rank[i]]);
  59. end;
  60. p:=;
  61. rank[sa[]]:=;
  62. for i:= to n do
  63. begin
  64. if (y[sa[i]]<>y[sa[i-]]) or (y[sa[i]+j]<>y[sa[i-]+j]) then inc(p);
  65. rank[sa[i]]:=p;
  66. end;
  67. m:=p;
  68. j:=j shl ;
  69. end;
  70. h[]:=;
  71. p:=;
  72. for i:= to n do
  73. begin
  74. if rank[i]= then continue;
  75. j:=sa[rank[i]-];
  76. while (i+p<=n) and (j+p<=n) and (s[i+p]=s[j+p]) do inc(p);
  77. h[rank[i]]:=p;
  78. if p> then dec(p);
  79. end;
  80. end;
  81.  
  82. function solve(l,r:longint):boolean;
  83. var i,t:longint;
  84. begin
  85. fillchar(v,sizeof(v),false);
  86. t:=;
  87. for i:=l to r do
  88. if (loc[sa[i]]<>-) then
  89. if not v[loc[sa[i]]] then
  90. begin
  91. inc(t);
  92. v[loc[sa[i]]]:=true;
  93. end;
  94. if t>k shr then exit(true) else exit(false);
  95. end;
  96.  
  97. function check(len:longint):boolean;
  98. var b,e,i:longint;
  99. fl:boolean;
  100.  
  101. begin
  102. fl:=false;
  103. b:=;
  104. e:=;
  105. for i:= to n do
  106. begin
  107. if h[i]>=len then inc(e)
  108. else begin
  109. if solve(b,e) then
  110. begin
  111. if not fl then tot:=;
  112. fl:=true;
  113. inc(tot);
  114. ans[tot]:=sa[b];
  115. end;
  116. b:=i;
  117. e:=i;
  118. end;
  119. end;
  120. if b<e then //注意收尾
  121. begin
  122. if solve(b,e) then
  123. begin
  124. if not fl then tot:=;
  125. inc(tot);
  126. fl:=true;
  127. ans[tot]:=sa[b];
  128. end;
  129. end;
  130. exit(fl);
  131. end;
  132.  
  133. begin
  134. readln(k);
  135. while k<> do
  136. begin
  137. s:='';
  138. r:=;
  139. m:=;
  140. for i:= to k do
  141. begin
  142. readln(ch);
  143. if r<length(ch) then r:=length(ch);
  144. for j:= to length(ch) do
  145. begin
  146. inc(m);
  147. loc[m]:=i;
  148. end;
  149. inc(m);
  150. loc[m]:=-;
  151. s:=s+ch+chr(i);
  152. end;
  153. n:=length(s);
  154. suffix;
  155. l:=;
  156. len:=;
  157. while l<=r do
  158. begin
  159. m:=(l+r) shr ;
  160. if check(m) then
  161. begin
  162. len:=m;
  163. l:=m+;
  164. end
  165. else r:=m-;
  166. end;
  167. if k= then
  168. begin
  169. writeln(s);
  170. writeln;
  171. end
  172. else if len= then
  173. begin
  174. writeln('?');
  175. writeln;
  176. end
  177. else begin
  178. for i:= to tot do
  179. begin;
  180. for j:=ans[i] to ans[i]+len- do
  181. write(s[j]);
  182. writeln;
  183. end;
  184. writeln;
  185. end;
  186. readln(k);
  187. end;
  188. end.

poj3294的更多相关文章

  1. 【POJ3294】 Life Forms (后缀数组+二分)

    Life Forms Description You may have wondered why most extraterrestrial life forms resemble humans, d ...

  2. 【poj3294】 Life Forms

    http://poj.org/problem?id=3294 (题目链接) 题意 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. Solution 后缀数组论文题.. 将 n 个字符串 ...

  3. POJ3294 Life Forms —— 后缀数组 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-3294 Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total ...

  4. 【POJ3294】Life Forms(后缀数组,二分)

    题意: n<=100 len[i]<=1000 思路:这是一道论文题 ..]of longint; ch:..]of ansistring; n,n1,l,r,mid,last,i,j,m ...

  5. poj3294 出现次数大于n/2 的公共子串

    Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13063   Accepted: 3670 Descr ...

  6. POJ3294 Life Forms(后缀数组)

    引用罗穗骞论文中的话: 将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组.然后二分答案,用和例3 同样的方法将后缀分成若干组,判断每组的后缀是否出现在不小于k 个的原串中 ...

  7. poj3294 Life Forms(后缀数组)

    [题目链接] http://poj.org/problem?id=3294 [题意] 多个字符串求出现超过R次的最长公共子串. [思路] 二分+划分height,判定一个组中是否包含不小于R个不同字符 ...

  8. poj3294 --Life Forms

    Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12483   Accepted: 3501 Descr ...

  9. Life Forms (poj3294 后缀数组求 不小于k个字符串中的最长子串)

    (累了,这题做了很久!) Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8683   Accepted ...

随机推荐

  1. C# QRCode、DataMatrix和其他条形码的生成和解码软件

    今天制造了一个C#的软件,具体是用于生成二维码和条形码的,包括常用的QRCode.DataMatrix.Code128.EAN-8等等. 使用的第三方类库是Zxing.net和DataMatrix.n ...

  2. dhcp源码编译支持4G上网卡

    1. tar xvzf dhcp-4.2.5-P1.tar.gz 2. ./configure --host=arm-linux ac_cv_file__dev_random=yes 3. vi bi ...

  3. Spring MVC 中的 forward 和 redirect

    Spring MVC 中,我们在返回逻辑视图时,框架会通过 viewResolver 来解析得到具体的 View,然后向浏览器渲染.假设逻辑视图名为 hello,通过配置,我们配置某个 ViewRes ...

  4. ubuntu14.04使用wubi安装出错

    使用wubi安装后,进入系统是总是提示/分区加载异常,无法正常进入系统. 参考解决方案来自 http://jingyan.baidu.com/article/0aa22375bbffbe88cc0d6 ...

  5. ASP.NET 相关小知识

    后台修改前台html控件属性 添加 runat=server ,后台获取// 客户端隐藏 a.Attributes[ "style "] = "display:none ...

  6. 2013年10月13日学习:SQL通过图形化界面创建表

    通过SQL2005创建表的方式有两种: 1.通过图形化用户界面来创建表.比较容易出问题,不稳定,容易点错了.不推荐 2.通过命令来创建.大牛都是这样做的,比较好. 通过图形化界面创建:以创建员工表为例 ...

  7. .NET多线程同步方法详解

    .NET多线程同步方法详解(一):自由锁(InterLocked) .NET多线程同步方法详解(二):互斥锁(lock) NET多线程同步方法详解(三):读写锁(ReadWriteLock) .NET ...

  8. (五)Struts2 标签

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 标签简介 Struts2 自己封装了一套标签,比JSTL ...

  9. npm install express -g出错

    npm ERR! Windows_NT npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program ...

  10. jquery前端性能优化(持续添加。。。)

    1.选择器的使用 (1)$('#id')   使用id来定位dom元素是性能最高的方法.jQuery底层将直接调用本地方法document.getElementById().如果id直接可以找到所要对 ...