【POJ3415】 Common Substrings (SA+单调栈)
这道是求长度不小于 k 的公共子串的个数...很不幸,我又TLE了...
解法参考论文以及下面的链接
http://www.cnblogs.com/vongang/archive/2012/11/20/2778481.html
http://hi.baidu.com/fpkelejggfbfimd/item/5c76cfcba28fba26e90f2ea6
- const maxn=;
- var
- c,h,rank,sa,x,y,stack:array[..maxn] of longint;
- n,k,top:longint;
- a,b,d:int64;
- s1,s2:ansistring;
- function max(x,y:longint):longint; begin if x>y then exit(x) else exit(y); end;
- function min(x,y:longint):longint; begin if x<y then exit(x) else exit(y); end;
- procedure make;
- var p,i,tot:longint;
- begin
- p:=;
- while p<n do
- begin
- fillchar(c,sizeof(c),);
- for i:= to n-p do y[i]:=rank[i+p];
- for i:= n-p+ to n do y[i]:=;
- for i:= to n do inc(c[y[i]]);
- for i:= to n do inc(c[i],c[i-]);
- for i:= to n do
- begin
- sa[c[y[i]]]:=i;
- dec(c[y[i]]);
- end;
- fillchar(c,sizeof(c),);
- for i:= to n do x[i]:=rank[i];
- for i:= to n do inc(c[x[i]]);
- for i:= to n do inc(c[i],c[i-]);
- for i:= n downto do
- begin
- y[sa[i]]:=c[x[sa[i]]];
- dec(c[x[sa[i]]]);
- end;
- for i:= to n do sa[y[i]]:=i;
- tot:=;
- rank[sa[]]:=;
- for i:= to n do
- begin
- if (x[sa[i]]<>x[sa[i-]]) or (x[sa[i]+p]<>x[sa[i-]+p]) then inc(tot);
- rank[sa[i]]:=tot;
- end;
- p:=p<<;
- end;
- for i:= to n do sa[rank[i]]:=i;
- end;
- procedure makeh(s:ansistring);
- var i,j,p:longint;
- begin
- h[]:=; p:=;
- for i:= to n do
- begin
- p:=max(p-,);
- if rank[i]= then continue;
- j:=sa[rank[i]-];
- while (i+p<=n) and (j+p<=n) and (s[i+p]=s[j+p]) do inc(p);
- h[rank[i]]:=p;
- end;
- end;
- procedure init(s:ansistring);
- var i,tot:longint;
- ch:char;
- begin
- n:=length(s);
- for i:= to n do c[i]:=;
- for i:= to n do x[i]:=ord(s[i]);
- for i:= to n do inc(c[x[i]]);
- for i:= to do inc(c[i],c[i-]);
- for i:= to n do
- begin
- sa[c[x[i]]]:=i;
- dec(c[x[i]]);
- end;
- rank[sa[]]:=;
- tot:=;
- for i:= to n do
- begin
- if x[sa[i]]<>x[sa[i-]] then inc(tot);
- rank[sa[i]]:=tot;
- end;
- fillchar(x,sizeof(x),);
- fillchar(y,sizeof(y),);
- make;
- makeh(s);
- end;
- function calc(s:ansistring):int64;
- var ctb,i,m,ht,top:longint;
- ans:int64;
- begin
- ans:=;
- init(s);
- h[]:=k-; h[n+]:=k-;
- top:=; i:=;
- stack[]:=;
- while i<=n+ do
- begin
- ht:=h[stack[top]];
- if ((h[i]<k) and (top=)) or (h[i]=ht) then inc(i)
- else if h[i]>ht then begin inc(top);stack[top]:=i; inc(i); end
- else
- begin
- m:=i-stack[top]+;
- if (h[i]>=k) and (h[i]>h[stack[top-]]) then
- begin
- ctb:=ht-h[i];
- h[stack[top]]:=h[i];
- end
- else
- begin
- ctb:=ht-h[stack[top-]];
- dec(top);
- end;
- inc(ans,(int64(m)*int64(m-) div ) *int64(ctb));
- end
- end;
- exit(ans);
- end;
- Begin
- readln(k);
- while k<> do
- begin
- readln(s1);
- a:=calc(s1);
- readln(s2);
- b:=calc(s2);
- s1:=s1+'$'+s2;
- d:=calc(s1);
- writeln(d-a-b);
- readln(k);
- end;
- End.
【POJ3415】 Common Substrings (SA+单调栈)的更多相关文章
- POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K ...
- Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...
- poj3415 Common Substrings(后缀数组,单调栈 | 后缀自动机)
[题目链接] http://poj.org/problem?id=3415 [题意] A与B长度至少为k的公共子串个数. [思路] 基本思想是将AB各个后缀的lcp-k+1的值求和.首先将两个字符串拼 ...
- POJ3415 Common Substrings 【后缀数组 + 单调栈】
常见的子串 时间限制: 5000MS 内存限制: 65536K 提交总数: 11942 接受: 4051 描述 字符串T的子字符串被定义为: Ť(我,ķ)= Ť 我 Ť 我 1 ... Ť I ...
- poj 3415 Common Substrings【SA+单调栈】
把两个串中间加一个未出现字符接起来,然后求SA 然后把贡献统计分为两部分,在排序后的后缀里,属于串2的后缀和排在他前面属于串1的后缀的贡献和属于串1的后缀和排在他前面属于串2的后缀的贡献 两部分分别作 ...
- Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)
原文链接https://www.cnblogs.com/zhouzhendong/p/9256033.html 题目传送门 - CF873F 题意 给定长度为 $n$ 的字符串 $s$,以及给定这个字 ...
- 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 ...
- Codeforces 1073G Yet Another LCP Problem $SA$+单调栈
题意 给出一个字符串\(s\)和\(q\)个询问. 每次询问给出两个长度分别为\(k,l\)的序列\(a\)和序列\(b\). 求\(\sum_{i=1}^{k}\sum_{j=1}^{l}lcp(s ...
- BZOJ3238 [Ahoi2013]差异 SA+单调栈
题面 戳这里 题解 考虑把要求的那个东西拆开算,前面一个东西像想怎么算怎么算,后面那个东西在建出\(height\)数组后相当于是求所有区间\(min\)的和*2,单调栈维护一波即可. #includ ...
随机推荐
- 【例题收藏】◇例题·V◇ Gap
◇例题·V◇ Gap 搜索训练开始了……POJ的数据比ZOJ强多了!!看来不得不写正解了 +传送门+ ◇ 题目 <简要翻译> 有一个四行九列的矩阵——在第1~4行.2~8列上填上数字 11 ...
- JQuery实现父级选择器(广告实现)
效果图如下: HTML代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- 原生js获取页面中所有checkbox
var inputs = document.getElementsByTagName("input");//获取所有的input标签对象 var checkboxArray = [ ...
- ethereum(以太坊)(一)
从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛.一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受.有难 ...
- dts--framework(二)
Framwork下个文件中包含的函数 packet.py LayersTypes = { ', 'arp', 'lldp'], # ipv4_ext_unknown, ipv6_ext_unknown ...
- kangle环境liunx一键安装脚本
1.kangle官方脚本 linux下easypanel版本安装及升级(集成了kangle web 服务器和mysql,仅支持centos 5和centos 6)执行下面的命令即可,安装程序将自动安装 ...
- python学习之控制流1
配置环境:python 3.6 python编辑器:pycharm 代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- # 控制流: # 1.布尔值: ...
- Python入门必学:数据类型和变量的用法
什么是数据类型?计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据, ...
- QWidget 自带的最大化,最小化,关闭按键的设置
使用函数 setWindowFlags 参数: CustomizeWindowHint 去掉窗口所有自带按钮 Qt::CustomizeWindowHint | Qt::WindowCloseButt ...
- js简单的获取与输出
js获取标签内容和输出内容到页面 获取: html: <select id="choiceSelect" onchange="changeImg()"&g ...