• Codeforces Round #269 (Div. 2) D


    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
    ",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
    ");
     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
    ",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
    ",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 }
  • 相关阅读:
    How to Automate IIS 7 Configuration with .NET
    windows phone 学习
    Redis VS. Memcached
    ROWNUM使用方法
    摘自----聊聊盛大的经验值
    理解Sql语句
    Odp.net 动态创建变量in
    ORACLE PLSQL DEVELOPER 中显示科学计数法
    http error 500.22 internal server error
    Resharper8 visual studio 中文版 快捷键无法使用
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4003053.html
Copyright © 2020-2023  润新知