bzoj1729: [Usaco2005 dec]Cow Patterns 牛的模式匹配
Description
Input
Output
维护第一个串在每个长度为K的区间内对应位置的排名的hash,与第二个串的排名的hash比较
具体可以用树状数组维护小于指定数的数的个数和大于指定数的数对hash值的贡献,详见代码
#include<cstdio>
typedef unsigned long long u64;
int n,k,s;
int t1[],t2[],a[],b[],as[],ap=;
u64 pp[],h2=,h1=,F[],fs=;
const u64 p=;
void add(int*f,int w,int v){
for(;w<=s;w+=w&-w)f[w]+=v;
}
int sum(int*f,int w){
int v=;
for(;w;w-=w&-w)v+=f[w];
return v;
}
void add(int w,u64 v){
fs+=v;
for(;w<=s;w+=w&-w)F[w]+=v;
}
u64 sum(int w){
u64 v=;
for(;w;w-=w&-w)v+=F[w];
return fs-v;
}
char buf[*],*ptr=buf-;
int _(){
int x=,c=*++ptr;
while(c<)c=*++ptr;
while(c>)x=x*+c-,c=*++ptr;
return x;
}
int main(){
fread(buf,,sizeof(buf),stdin);
n=_();k=_();s=_();
pp[]=;
for(int i=;i<=n;++i)pp[i]=pp[i-]*p;
for(int i=;i<=n;++i)a[i]=_();
for(int i=;i<=k;++i)b[i]=_();
for(int i=;i<=k;++i){
add(t1,a[i],);
add(a[i],pp[i]);
add(t2,b[i],);
}
for(int i=;i<=k;++i){
h1+=pp[i]*sum(t1,a[i]-);
h2+=pp[i]*sum(t2,b[i]-);
}
if(h1==h2)as[ap++]=;
for(int i=k+,j;i<=n;++i){
j=i-k;
h1-=pp[j]*sum(t1,a[j]-);
h1-=sum(a[j]);
add(t1,a[j],-);
add(a[j],-pp[j]);
h1+=sum(a[i]);
h1+=pp[i]*sum(t1,a[i]-);
add(t1,a[i],);
add(a[i],pp[i]);
if(h1==h2*pp[j])as[ap++]=j+;
}
printf("%d\n",ap);
for(int i=;i<ap;++i)printf("%d\n",as[i]);
return ;
}
bzoj1729: [Usaco2005 dec]Cow Patterns 牛的模式匹配的更多相关文章
- BZOJ 1729: [Usaco2005 dec]Cow Patterns 牛的模式匹配
Description 约翰的N(1≤N≤100000)只奶牛中出现了K(1≤K≤25000)只爱惹麻烦的坏蛋.奶牛们按一定的顺序排队的时候,这些坏蛋总会站在一起.为了找出这些坏蛋,约翰让他的奶牛排好 ...
- 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划
[BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...
- BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 414 Solved: ...
- BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MB Description Farm ...
- [Usaco2005 Dec]Cleaning Shifts 清理牛棚 (DP优化/线段树)
[Usaco2005 Dec] Cleaning Shifts 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new ...
- 【BZOJ1731】[Usaco2005 dec]Layout 排队布局 差分约束
[BZOJ1731][Usaco2005 dec]Layout 排队布局 Description Like everyone else, cows like to stand close to the ...
- BZOJ1679: [Usaco2005 Jan]Moo Volume 牛的呼声
1679: [Usaco2005 Jan]Moo Volume 牛的呼声 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 723 Solved: 346[ ...
- BZOJ 1679: [Usaco2005 Jan]Moo Volume 牛的呼声( )
一开始直接 O( n² ) 暴力..结果就 A 了... USACO 数据是有多弱 = = 先sort , 然后自己再YY一下就能想出来...具体看code --------------------- ...
- 3893: [Usaco2014 Dec]Cow Jog
3893: [Usaco2014 Dec]Cow Jog Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 174 Solved: 87[Submit] ...
随机推荐
- LeetCode OJ : Different Ways to Add Parentheses(在不同位置增加括号的方法)
Given a string of numbers and operators, return all possible results from computing all the differen ...
- 防火墙---iptables
一.iptables的说明及环境安装 (1)理论基础:当主机收到一个数据包后,数据包先在内核空间中处理,若发现目的地址是自身,则传到用户空间中交给对应的应用程序处理,若发现目的不是自身,则会将包丢弃或 ...
- Linux系统下超级用户密码的修改
1)重启系统:在虚拟机刚启动界面,不停地按上下键,停止系统的自动引导(界面底部有提示) 2) 按 e 进入编辑模式 3) 编辑内容如下:完成后按Ctrl+x (具体编辑内容为下图:删除倒数第三行 ...
- location.host 与 location.hostname 的区别
JavaScript 中,大多数情况下,我们不会发现 location.host 与 location.hostname 的区别,因为大多数情况下,我们的网页用的是 80 端口. 他们的区别: loc ...
- Tornado 自定义session,与一致性哈希 ,基于redis 构建分布式 session框架
Tornado 自定义session,与一致性哈希 ,基于redis 构建分布式 session import tornado.ioloop import tornado.web from myhas ...
- 启动Windows防火墙提示“0x8007042c"
win8.1 启动防火墙是报错:启动Windows防火墙提示“0x8007042c" 一.检查服务 1,右击开始->运行->输入“services.msc” 打开服务 在框中找到 ...
- artDialog的使用
用法一:github上下载包:https://github.com/aui/artDialog seajs方法使用 <!doctype html> <h ...
- Postfix常用命令和邮件队列管理(queue)
本文主要介绍一下postfix的常用命令及邮件队列的管理: Postfix有以下四种邮件队列,均由管理队列的进程统一进行管理: maildrop:本地邮件放置在maildrop中,同时也被拷贝到inc ...
- I.MX6 Linux eGTouch TouchScreen porting
I.MX6 Linux eGTouch TouchScreen porting 一.Download Driver: http://www.eeti.com.tw/drivers_Linux.html ...
- skywalking探针tomcat8.0.28报错解决
在部署skywalking agent的时候遇到一个异常 环境如下: tomcat8.0.28 catalina.out 日志报如下错误 30-Apr-2019 10:25:57.664 INFO [ ...