Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp

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<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<string>
 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值