CodeForces 471D MUH and Cube Walls -KMP

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.


  题目大意:给定一个序列A,它的某一个子串通过每个元素加上一个x,使得这个子串和序列B相同,问有多少个这样的开始位置不同的子串。

  如果没有加上x这个条件就是KMP模板,那么只能找个不会变化的量来进行KMP,这个量就是后一项和前一项的差(没有后一项的的话不管)。特殊地,当m = 1时,结果为n。

Code

  1 /**
  2  * codeforces
  3  * Problem#471D
  4  * Accepted
  5  * Time:46ms
  6  * Memory:3720k
  7  */
  8 #include<iostream>
  9 #include<cstdio>
 10 #include<cctype>
 11 #include<cstring>
 12 #include<cstdlib>
 13 #include<fstream>
 14 #include<sstream>
 15 #include<algorithm>
 16 #include<map>
 17 #include<ctime>
 18 #include<set>
 19 #include<queue>
 20 #include<vector>
 21 #include<stack>
 22 using namespace std;
 23 typedef bool boolean;
 24 #define INF 0xfffffff
 25 #define smin(a, b) a = min(a, b)
 26 #define smax(a, b) a = max(a, b)
 27 #ifndef WIN32
 28 #define AUTO "%lld"
 29 #else
 30 #define AUTO "%I64d"
 31 #endif
 32 template<typename T>
 33 inline void readInteger(T& u){
 34     char x;
 35     int aFlag = 1;
 36     while(!isdigit((x = getchar())) && x != '-');
 37     if(x == '-'){
 38         x = getchar();
 39         aFlag = -1;
 40     }
 41     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
 42     ungetc(x, stdin);
 43     u *= aFlag;
 44 }
 45 
 46 int n, m;
 47 int *S, *T;
 48 int *f;
 49 
 50 inline void init() {
 51     readInteger(n);
 52     readInteger(m);
 53     S = new int[(const int)n];
 54     T = new int[(const int)m];
 55     int last = 0;
 56     for(int i = 1, a; i <= n; i++) {
 57         readInteger(a);
 58         if(i > 1) {
 59             S[i - 2] = a - last;
 60         }
 61         last = a;
 62     }
 63     for(int i = 1, a; i <= m; i++) {
 64         readInteger(a);
 65         if(i > 1) {
 66             T[i - 2] = a - last;
 67         }
 68         last = a;
 69     }
 70     n--, m--;
 71 }
 72 
 73 inline void getFail() {
 74     f = new int[(const int)(m + 1)];
 75     f[0] = f[1] = 0;
 76     for(int i = 1, j; i < m; i++) {
 77         j = f[i];
 78         while(j > 0 && T[i] != T[j])    j = f[j];
 79         f[i + 1] = (T[i] == T[j]) ? (j + 1) : (0);
 80     }
 81 }
 82 
 83 int res = 0;
 84 inline void kmp() {
 85     getFail();
 86     for(int i = 0, j = 0; i < n; i++) {
 87         while(j > 0 && S[i] != T[j])    j = f[j];
 88         if(S[i] == T[j] && j < m)    j++;
 89         if(j == m)    res++, j = f[j];
 90     }
 91 }
 92 
 93 inline void solve() {
 94     if(m == 0) {
 95         printf("%d", n + 1);
 96         return;
 97     }
 98     kmp();
 99     printf("%d", res);
100 }
101 
102 int main() {
103     init();
104     solve();
105     return 0;
106 }
原文地址:https://www.cnblogs.com/yyf0309/p/6379812.html