D-muh and cube wballs
Time limit:2000 ms
Memory limit:262144kb
64bit Io format:% I64d & % i64usubmit status practice codeforces 471d
Description
Polar bears menshykov and uslada from the zoo of St. petersburg and elephant hsf-e 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.
Hsf-e was the first to finish making his wall. He called his wall an elephant. The wall consistsWTowers. The bears also finished making their wall but they didn't give it a name. Their wall consistsNTowers. hsf-e 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 segmentWContiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in hsf-e's wall. in order to see as your elephants as possible, hsf-e 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 hsf-e can "see an elephant ".
Input
The first line contains two integersNAndW(1 digit ≤ DigitN, Bytes,WLimit ≤ limit 2 · 105)-the number of towers in the bears and the elephant's Wils correspondingly. The second line containsNIntegersAI(1 digit ≤ DigitAILimit ≤ limit 109)-the heights of the towers in the bears 'Wall. The third line containsWIntegersBI(1 digit ≤ DigitBILimit ≤ limit 109)-the heights of the towers in the elephant's wall.
Output
Print the number of segments in the bears 'Wall where hsf-e 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 hsf-e's wall from the sample, the picture to the right shows the bears 'Wall. The segments where hsf-e can "see an elephant" are in gray.
The first 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 }
Codeforces round #269 (Div. 2) D-muh and cube wallkmp