zoukankan      html  css  js  c++  java
  • CodeForces–471D--MUH and Cube Walls(KMP)

    Time limit         2000 ms 
    Memory limit  262144 kB

    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".

    Example

    Input

    13 5
    2 4 5 5 4 3 2 2 2 3 3 2 1
    3 4 4 3 2

    Output

    2
    Note

    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,可是傻B地直接暴力的。。。。果断TLE

    后来看了题解,很巧妙的处理了一下:

    基于一段连续区间无论怎么变他们的相对位置不会变,所以分别将两个序列相邻的元素作差,一段区间的相对位置不会变,所以正好可以用差值去匹配。

    代码:

      1 #include<bits/stdc++.h>
      2 const int N=2e5+7;
      3 int len1,len2,a[N],b[N],f[N],p[N];
      4 void kmp_pre(int *x,int m,int *next)//以下感谢kuangbin神主提供的模板。
      5 {
      6     int i,j;
      7     j=next[0]=-1;
      8     i=0;
      9     while(i<m)
     10     {
     11         while(-1!=j&&x[i]!=x[j]) j=next[j];
     12         next[++i]=++j;
     13     }
     14 }
     15 void prekmp(int *x,int m,int *kmpnext)
     16 {
     17     int i,j=-1;
     18     kmpnext[0]=-1;
     19     i=0;
     20     while(i<m)
     21     {
     22         while(-1!=j&&x[i]!=x[j]) j=kmpnext[j];
     23         if(x[++i]==x[++j]) kmpnext[i]=kmpnext[j];
     24         else kmpnext[i]=j;
     25     }
     26 }
     27 int next[N];
     28 int kmp_count(int *x,int m,int *y,int n)
     29 {
     30     int i,j,ans=0;
     31     kmp_pre(x,m,next);
     32     i=j=0;
     33     while(i<n)
     34     {
     35         while(-1!=j&&y[i]!=x[j]) j=next[j];
     36         i++,j++;
     37         if(j>=m)
     38         {
     39             ans++;
     40             j=next[j];
     41         }
     42     }
     43     return ans;
     44 }
     45 int main()
     46 {
     47     int w,n;
     48     while(~scanf("%d%d",&w,&n))
     49     {
     50         len1=0,len2=0;
     51         for(int i=0;i<w;i++)
     52         {
     53             scanf("%d",&a[i]);
     54             if(i) f[len1++]=a[i]-a[i-1];
     55         }
     56         for(int i=0;i<n;i++)
     57          {
     58             scanf("%d",&b[i]);
     59             if(i) p[len2++]=b[i]-b[i-1];
     60          }
     61          if(n==1)
     62          {
     63              printf("%d
    ",w);
     64              continue;
     65          }
     66       int ans=kmp_count(p,len2,f,len1);
     67       printf("%d
    ",ans);
     68     }
     69    return 0;
     70 }
    View Code
  • 相关阅读:
    WM_COMMAND 和 WM_NOTIFY 的区别
    C 游戏所要看的书
    Source Insight中文字体设置
    在 windows7 中使用 vs2003 时,“在文件中查找”导致无响应的问题
    解决VS2008 调试启动特别慢
    c++ 浅谈 new 的使用
    Access界面基础操作
    与孩子一起学编程12章
    YT工作日志-0911
    两种方式遍历二叉树--递归方式和非递归方式
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/6701653.html
Copyright © 2011-2022 走看看