博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp
阅读量:6097 次
发布时间:2019-06-20

本文共 3865 字,大约阅读时间需要 12 分钟。

D -
 MUH and Cube Walls
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
   

Description

Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of different heights.

Horace was the first to finish making his wall. He called his wall an elephant. The wall consists of w towers. The bears also finished making their wall but they didn't give it a name. Their wall consists of n towers. Horace looked at the bears' tower and wondered: in how many parts of the wall can he "see an elephant"? He can "see an elephant" on a segment of w contiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in Horace's wall. In order to see as many elephants as possible, Horace can raise and lower his wall. He even can lower the wall below the ground level (see the pictures to the samples for clarification).

Your task is to count the number of segments where Horace can "see an elephant".

Input

The first line contains two integers n and w (1 ≤ n, w ≤ 2·105) — the number of towers in the bears' and the elephant's walls correspondingly. The second line contains n integers ai (1 ≤ ai ≤ 109) — the heights of the towers in the bears' wall. The third line contains w integers bi (1 ≤ bi ≤ 109) — the heights of the towers in the elephant's wall.

Output

Print the number of segments in the bears' wall where Horace can "see an elephant".

Sample Input

Input
13 5 2 4 5 5 4 3 2 2 2 3 3 2 1 3 4 4 3 2
Output
2

Hint

The picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can "see an elephant" are in gray.

 

 

第一个kmp。。。。

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 11 #define N 200005 12 #define M 15 13 #define mod 10000007 14 //#define p 10000007 15 #define mod2 100000000 16 #define ll long long 17 #define LL long long 18 #define maxi(a,b) (a)>(b)? (a) : (b) 19 #define mini(a,b) (a)<(b)? (a) : (b) 20 21 using namespace std; 22 23 int T; 24 int n; 25 int w; 26 int a[N],b[N]; 27 int c[N],d[N]; 28 int next[N]; 29 int ans; 30 31 void get_next() 32 { 33 int i = 1, j = 0; 34 next[1] = 0; 35 while(i <=w-2) 36 { 37 if(j == 0 || d[i] == d[j]) 38 { 39 j++; 40 i++; 41 next[i] = j; 42 } 43 else 44 j = next[j]; 45 } 46 // for(i=1;i<=w-1;i++){ 47 // printf(" i=%d d=%d next=%d\n",i,d[i],next[i]); 48 // } 49 } 50 51 void ini() 52 { 53 int i; 54 ans=0; 55 for(i=1;i<=n;i++){ 56 scanf("%d",&a[i]); 57 } 58 for(i=1;i<=n-1;i++){ 59 c[i]=a[i+1]-a[i]; 60 } 61 // for(i=1;i<=n-1;i++){ 62 // printf(" %d",c[i]); 63 // } 64 for(i=1;i<=w;i++){ 65 scanf("%d",&b[i]); 66 } 67 for(i=1;i<=w-1;i++){ 68 d[i]=b[i+1]-b[i]; 69 } 70 } 71 72 73 void solve() 74 { 75 int i,j; 76 // printf(" 1221\n"); 77 if(w==1){ 78 ans=n; 79 return ; 80 } 81 i=1,j=1; 82 while(i<=n-1){ 83 // printf(" i=%d j=%d ans=%d\n",i,j,ans); 84 if(j==0 || c[i]==d[j]){ 85 if(j==w-1){ 86 ans++; 87 //i++; 88 j=next[j]; 89 } 90 else{ 91 i++; 92 j++; 93 } 94 } 95 else{ 96 j=next[j]; 97 } 98 } 99 }100 101 void out()102 {103 printf("%d\n",ans);104 }105 106 int main()107 {108 //freopen("data.in","r",stdin);109 //freopen("data.out","w",stdout);110 //scanf("%d",&T);111 // for(int ccnt=1;ccnt<=T;ccnt++)112 // while(T--)113 while(scanf("%d%d",&n,&w)!=EOF)114 {115 // if(n==0 && m==0) break;116 //printf("Case %d: ",ccnt);117 ini();118 get_next();119 solve();120 out();121 }122 123 return 0;124 }

 

转载于:https://www.cnblogs.com/njczy2010/p/4003053.html

你可能感兴趣的文章
java 中getDeclaredFields() 与getFields() 的区别
查看>>
熟练掌握doc命令下的文件操作
查看>>
Oracle中drop user和drop user cascade的区别
查看>>
【Linux】linux经常使用基本命令
查看>>
Java 内存区域和GC机制
查看>>
更新代码和工具,组织起来,提供所有博文(C++,2014.09)
查看>>
HTML模块化:使用HTML5 Boilerplate模板
查看>>
登记申请汇总
查看>>
Google最新截屏案例详解
查看>>
2015第31周一
查看>>
2015第31周日
查看>>
在使用EF开发时候,遇到 using 语句中使用的类型必须可隐式转换为“System.IDisposable“ 这个问题。...
查看>>
PHP使用DES进行加密和解密
查看>>
Oracle 如何提交手册Cluster Table事务
查看>>
BeagleBone Black第八课板:建立Eclipse编程环境
查看>>
在服务器上用Fiddler抓取HTTPS流量
查看>>
文件类似的推理 -- 超级本征值(super feature)
查看>>
【XCode7+iOS9】http网路连接请求、MKPinAnnotationView自定义图片和BitCode相关错误--备用...
查看>>
各大公司容器云的技术栈对比
查看>>
记一次eclipse无法启动的排查过程
查看>>