这道是求长度不小于 k 的公共子串的个数...很不幸,我又TLE了...

解法参考论文以及下面的链接

http://www.cnblogs.com/vongang/archive/2012/11/20/2778481.html

http://hi.baidu.com/fpkelejggfbfimd/item/5c76cfcba28fba26e90f2ea6

  1. const maxn=;
  2. var
  3. c,h,rank,sa,x,y,stack:array[..maxn] of longint;
  4. n,k,top:longint;
  5. a,b,d:int64;
  6. s1,s2:ansistring;
  7.  
  8. function max(x,y:longint):longint; begin if x>y then exit(x) else exit(y); end;
  9. function min(x,y:longint):longint; begin if x<y then exit(x) else exit(y); end;
  10.  
  11. procedure make;
  12. var p,i,tot:longint;
  13. begin
  14. p:=;
  15. while p<n do
  16. begin
  17. fillchar(c,sizeof(c),);
  18. for i:= to n-p do y[i]:=rank[i+p];
  19. for i:= n-p+ to n do y[i]:=;
  20. for i:= to n do inc(c[y[i]]);
  21. for i:= to n do inc(c[i],c[i-]);
  22. for i:= to n do
  23. begin
  24. sa[c[y[i]]]:=i;
  25. dec(c[y[i]]);
  26. end;
  27. fillchar(c,sizeof(c),);
  28. for i:= to n do x[i]:=rank[i];
  29. for i:= to n do inc(c[x[i]]);
  30. for i:= to n do inc(c[i],c[i-]);
  31. for i:= n downto do
  32. begin
  33. y[sa[i]]:=c[x[sa[i]]];
  34. dec(c[x[sa[i]]]);
  35. end;
  36. for i:= to n do sa[y[i]]:=i;
  37. tot:=;
  38. rank[sa[]]:=;
  39. for i:= to n do
  40. begin
  41. if (x[sa[i]]<>x[sa[i-]]) or (x[sa[i]+p]<>x[sa[i-]+p]) then inc(tot);
  42. rank[sa[i]]:=tot;
  43. end;
  44. p:=p<<;
  45. end;
  46. for i:= to n do sa[rank[i]]:=i;
  47. end;
  48.  
  49. procedure makeh(s:ansistring);
  50. var i,j,p:longint;
  51. begin
  52. h[]:=; p:=;
  53. for i:= to n do
  54. begin
  55. p:=max(p-,);
  56. if rank[i]= then continue;
  57. j:=sa[rank[i]-];
  58. while (i+p<=n) and (j+p<=n) and (s[i+p]=s[j+p]) do inc(p);
  59. h[rank[i]]:=p;
  60. end;
  61. end;
  62.  
  63. procedure init(s:ansistring);
  64. var i,tot:longint;
  65. ch:char;
  66. begin
  67. n:=length(s);
  68. for i:= to n do c[i]:=;
  69. for i:= to n do x[i]:=ord(s[i]);
  70. for i:= to n do inc(c[x[i]]);
  71. for i:= to do inc(c[i],c[i-]);
  72. for i:= to n do
  73. begin
  74. sa[c[x[i]]]:=i;
  75. dec(c[x[i]]);
  76. end;
  77. rank[sa[]]:=;
  78. tot:=;
  79. for i:= to n do
  80. begin
  81. if x[sa[i]]<>x[sa[i-]] then inc(tot);
  82. rank[sa[i]]:=tot;
  83. end;
  84. fillchar(x,sizeof(x),);
  85. fillchar(y,sizeof(y),);
  86. make;
  87. makeh(s);
  88. end;
  89.  
  90. function calc(s:ansistring):int64;
  91. var ctb,i,m,ht,top:longint;
  92. ans:int64;
  93. begin
  94. ans:=;
  95. init(s);
  96. h[]:=k-; h[n+]:=k-;
  97. top:=; i:=;
  98. stack[]:=;
  99. while i<=n+ do
  100. begin
  101. ht:=h[stack[top]];
  102. if ((h[i]<k) and (top=)) or (h[i]=ht) then inc(i)
  103. else if h[i]>ht then begin inc(top);stack[top]:=i; inc(i); end
  104. else
  105. begin
  106. m:=i-stack[top]+;
  107. if (h[i]>=k) and (h[i]>h[stack[top-]]) then
  108. begin
  109. ctb:=ht-h[i];
  110. h[stack[top]]:=h[i];
  111. end
  112. else
  113. begin
  114. ctb:=ht-h[stack[top-]];
  115. dec(top);
  116. end;
  117. inc(ans,(int64(m)*int64(m-) div ) *int64(ctb));
  118. end
  119. end;
  120. exit(ans);
  121. end;
  122.  
  123. Begin
  124. readln(k);
  125. while k<> do
  126. begin
  127. readln(s1);
  128. a:=calc(s1);
  129. readln(s2);
  130. b:=calc(s2);
  131. s1:=s1+'$'+s2;
  132. d:=calc(s1);
  133. writeln(d-a-b);
  134. readln(k);
  135. end;
  136. End.

【POJ3415】 Common Substrings (SA+单调栈)的更多相关文章

  1. POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数

    题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K ...

  2. Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...

  3. poj3415 Common Substrings(后缀数组,单调栈 | 后缀自动机)

    [题目链接] http://poj.org/problem?id=3415 [题意] A与B长度至少为k的公共子串个数. [思路] 基本思想是将AB各个后缀的lcp-k+1的值求和.首先将两个字符串拼 ...

  4. POJ3415 Common Substrings 【后缀数组 + 单调栈】

    常见的子串 时间限制: 5000MS   内存限制: 65536K 提交总数: 11942   接受: 4051 描述 字符串T的子字符串被定义为: Ť(我,ķ)= Ť 我 Ť 我 1 ... Ť I ...

  5. poj 3415 Common Substrings【SA+单调栈】

    把两个串中间加一个未出现字符接起来,然后求SA 然后把贡献统计分为两部分,在排序后的后缀里,属于串2的后缀和排在他前面属于串1的后缀的贡献和属于串1的后缀和排在他前面属于串2的后缀的贡献 两部分分别作 ...

  6. Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)

    原文链接https://www.cnblogs.com/zhouzhendong/p/9256033.html 题目传送门 - CF873F 题意 给定长度为 $n$ 的字符串 $s$,以及给定这个字 ...

  7. luogu2178/bzoj4199 品酒大会 (SA+单调栈)

    他要求的就是lcp(x,y)>=i的(x,y)的个数和a[x]*a[y]的最大值 做一下后缀和,就只要求lcp=i的了 既然lcp(x,y)=min(h[rank[x]+1],..,[h[ran ...

  8. Codeforces 1073G Yet Another LCP Problem $SA$+单调栈

    题意 给出一个字符串\(s\)和\(q\)个询问. 每次询问给出两个长度分别为\(k,l\)的序列\(a\)和序列\(b\). 求\(\sum_{i=1}^{k}\sum_{j=1}^{l}lcp(s ...

  9. BZOJ3238 [Ahoi2013]差异 SA+单调栈

    题面 戳这里 题解 考虑把要求的那个东西拆开算,前面一个东西像想怎么算怎么算,后面那个东西在建出\(height\)数组后相当于是求所有区间\(min\)的和*2,单调栈维护一波即可. #includ ...

随机推荐

  1. 【例题收藏】◇例题·V◇ Gap

    ◇例题·V◇ Gap 搜索训练开始了……POJ的数据比ZOJ强多了!!看来不得不写正解了 +传送门+ ◇ 题目 <简要翻译> 有一个四行九列的矩阵——在第1~4行.2~8列上填上数字 11 ...

  2. JQuery实现父级选择器(广告实现)

    效果图如下: HTML代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  3. 原生js获取页面中所有checkbox

    var inputs = document.getElementsByTagName("input");//获取所有的input标签对象 var checkboxArray = [ ...

  4. ethereum(以太坊)(一)

    从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛.一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受.有难 ...

  5. dts--framework(二)

    Framwork下个文件中包含的函数 packet.py LayersTypes = { ', 'arp', 'lldp'], # ipv4_ext_unknown, ipv6_ext_unknown ...

  6. kangle环境liunx一键安装脚本

    1.kangle官方脚本 linux下easypanel版本安装及升级(集成了kangle web 服务器和mysql,仅支持centos 5和centos 6)执行下面的命令即可,安装程序将自动安装 ...

  7. python学习之控制流1

    配置环境:python 3.6 python编辑器:pycharm 代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- # 控制流: # 1.布尔值: ...

  8. Python入门必学:数据类型和变量的用法

    什么是数据类型?计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据, ...

  9. QWidget 自带的最大化,最小化,关闭按键的设置

    使用函数 setWindowFlags 参数: CustomizeWindowHint 去掉窗口所有自带按钮 Qt::CustomizeWindowHint | Qt::WindowCloseButt ...

  10. js简单的获取与输出

    js获取标签内容和输出内容到页面 获取: html: <select id="choiceSelect" onchange="changeImg()"&g ...