poj3167
这道题是一道kmp的扩展版的好题
一串匹配一串很容易想到kmp,但是这里的匹配要求的是两个串的名次相同
显然名次是会变的,为了方便,我们可以换一种表达
对于两个等长的串的相同位置,名次相等就是在它之前比它小的数的个数一样,和它相等的数的个数一样
这个我们可以用树状数组维护一下(当然暴力好像也行)
然后匹配就行了
var ans,a,b,sal,eq:array[..] of longint;
next:array[..] of longint;
c:array[..] of longint;
tot,k,n,m,s,i,j:longint; function lowbit(x:longint):longint;
begin
exit(x and (-x));
end; procedure work(x,p:longint);
begin
while x<=s do
begin
inc(c[x],p);
x:=x+lowbit(x);
end;
end; function ask(x:longint):longint;
begin
ask:=;
while x> do
begin
ask:=ask+c[x];
x:=x-lowbit(x);
end;
end; begin
readln(n,m,s);
for i:= to n do
readln(a[i]);
for i:= to m do //预处理
begin
readln(b[i]);
work(b[i],);
sal[i]:=ask(b[i]-);
eq[i]:=ask(b[i]);
end;
fillchar(c,sizeof(c),);
i:=;
j:=;
next[]:=;
while i<=m do
begin
if (j=) or (ask(b[i]-)=sal[j]) and (ask(b[i])=eq[j]) then
begin
inc(i);
inc(j);
next[i]:=j;
if i>m then break;
work(b[i],);
end
else begin
for k:=i-j+ to i-next[j] do // 保证匹配的串位置一样
work(b[k],-);
j:=next[j];
end;
end;
fillchar(c,sizeof(c),);
i:=;
j:=;
work(a[],);
while (i<=n) do
begin
if (j=) or (ask(a[i]-)=sal[j]) and (ask(a[i])=eq[j]) then
begin
inc(i);
inc(j);
if i<=n then
work(a[i],);
end
else begin
for k:=i-j+ to i-next[j] do
work(a[k],-);
j:=next[j];
end;
if j>m then //注意这里要找出的是所有答案
begin
for k:=i-m to i-next[j] do
work(a[k],-);
inc(tot);
ans[tot]:=i-m;
j:=next[j];
end;
end;
writeln(tot);
for i:= to tot do
writeln(ans[i]);
end.
poj3167的更多相关文章
- poj3167(kmp)
题目链接: http://poj.org/problem?id=3167 题意: 给出两串数字 s1, s2, 求主串 s1 中的 s2 匹配数并输出每个匹配的开头位置. 区间 [l, r] 是 s2 ...
- 【POJ 3167】Cow Patterns (KMP+树状数组)
Cow Patterns Description A particular subgroup of K (1 <= K <= 25,000) of Farmer John's cows l ...
随机推荐
- WPF--ComboBox数据绑定
WPF--ComboBox数据绑定 0-在ComboBox中显示图片: <ComboBox Height="33" HorizontalAlignment="Rig ...
- proguard.cfg 配置文件
# ------------------------------------- # android 原始混淆模板 # ------------------------------------- # - ...
- jar打包通过exe4j转换成exe文件
去年的时候有用过,最近写java的时候偶然用到,mark一下,方便以后看 下载链接后面附上 首先我们在eclipse上打包成jar文件,我这里只把简单的截图贴出来,详细的可以自行百度 打包jar文件: ...
- javascript实现继承的6种方式
/*1.原型链继承*/ function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = funct ...
- IXListView的自我分析一
XListView是一个很不错的用来刷新和加载的控件,下拉刷新和上拉加载.目前这个控件已经没有更新,这个不重要,重要的是它确实还不错,之后可能一直有人在用. android没有提供原生的这类控件,需要 ...
- 浅析Activity不可见与透明
http://blog.csdn.net/lincyang/article/details/6868582 看见标题也许你会有疑问,不可见和透明不是一个意思吗? 从字面上看,这还真是差不多.但在Act ...
- phpmyadmin备份小问题
不要将imformation——shame或者mysql等备份,要有选择的备份表 关注我的新浪微博
- iOS程序员的自我修养之道
新技术的了解渠道 WWDC开发者大会视频 官方文档 General -> Guides -> iOS x.x API Diffs 程序员的学习 iOS技术的学习 官当文档 Sample C ...
- [LeetCode OJ] Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- div section article aside的理解
div 是一个大的容器 内部可以包含header main nav aside footer等标签 没有语义,多用于为脚本添加样式 section的语义比div语义强些,用于主题性比较强的内容,比如一 ...