【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 ...
随机推荐
- Linux - 用户环境变量的查看与设置
1. 查看当前有哪些环境变量 直接输入命令:env 2. 设置用户环境变量 输入命令:vim ~/.bash_profile,打开文件,输入如下内容: 范例(设置maven环境变量): export ...
- $.each() 循环遍历完后阻止再执行的办法
jquery each循环遍历完再执行的方法 因为each是异步的 所以要加计数器. query each循环遍历完再执行的方法 因为each是异步的 所以要加计数器.var eachcount=0; ...
- jupyter notebook中出现ValueError: signal only works in main thread 报错 即 长时间in[*] 解决办法
我在jupyter notebook中新建了一个基于py3.6的kernel用来进行tensorflow学习 但是在jupyter notebook中建立该kernel时,右上角总是显示 服务正在启动 ...
- POJ 2217 LCS(后缀数组)
Secretary Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1655 Accepted: 671 Descript ...
- proteus中蜂鸣器不响的原因
本文参考自https://blog.csdn.net/gin_love/article/details/51168369 此网站.在用proteus仿真报警电路时,发现蜂鸣器不响.后在网上找了 ...
- 17-比赛2 F - Fox And Two Dots (dfs)
Fox And Two Dots CodeForces - 510B ================================================================= ...
- 笔记-python-lib-contextlib
笔记-python-lib-contextlib 1. contextlib with 语句很好用,但不想每次都写__enter_-和__exit__方法: py标准库也为此提供了工具模块c ...
- 笔记-git-.gitignore
笔记-git-.gitignore 1. git忽略文件 有的文件不需要提交到公共仓库中,为此git提供了三种实现方式. gitignore文件 在项目的设置中指定排除文件 定义全局.git ...
- bootstrap重新设计按钮样式
1.将btn的样式换成以下的样式 2.思路: (1)将原来的btn样式设置color:#FFF,同时text-shadow设置,这样原来的btn样式就会变淡了,后面再加上新的样式就可以覆盖掉 注意:要 ...
- 页面引入外部字体ttf,如何提取所需要的ttf字体或者加载过慢的解决方法-1127更新
最近几天编写手机端的页面之后,文中需要华文行楷字体,在网上下载后,引入到了自己的前端页面,以为没有什么事了,继续码代码 @font-face { font-family:huawen; src: ur ...