codevs3945 Perfect Rubbing

Source: Internet
Author: User

3945 Perfect Rubbing

Codevs first game of the month race

Time limit: 1 s

Space limit: 256000 KB

Title Level: Golden Gold

Title Description Description

Small Q obtained a magical seal, the seal is wide n units in length, and the three edges of the seal are straight, while the other direction, for each unit width part, is the same straight, and parallel to the opposite direction of the edge, as shown in.

Small Q's seal has a not about the center symmetry of the graphic (not necessarily the qrz), he is now on a map to expand the printed, the map has a section of m unit length, approximate level of the boundary line, but the magnification to the unit length is still a certain high and low difference, but for the unit width of the part, Is the same as straight, perpendicular to the horizontal axis, as shown in.

Small Q hope that the edge of his seal on the side can coincide with the boundary line (can not be partially coincident, can not cross the boundary lines), he can now only rotate the seal 180 degrees or do not rotate (so that the seal may exist on both sides can be coincident with the boundary conditions), Then pan to the appropriate location, ask the small q how many feasible scenarios, the two schemes are defined as two different schemes with the seal printed patterns do not overlap.

Enter a description input Description

The first line, two positive integers n and M, represents the width of the seal and the length of the intercept boundary.

The second row n positive integers that represent the distance between the two parallel lines of the seal from left to right for each unit width.

The third row of M integers, representing the coordinates of the vertical direction in which the truncated boundary lines correspond to each unit width from left to right.

Outputs description Output Description

An integer that represents the number of types of feasible schemes.

Sample input to sample

5 12

3 4 4) 3 2

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

Sample output Sample Outputs

3

Data Size & Hint

For 30% of the data, there is n*m≤2*107.

For 60% of the data, there is n,m≤105.

For 100% of the data, there is n,m≤106, the absolute value of all numbers is not more than 109.

Ideas

String matches.

One side of the bump situation as a feature, if both sides of the same concave and convex condition can be the seal, then the problem is converted to find two bump conditions of the matching number, can be solved by KMP algorithm.

It is important to note that:

1, the bottom edge can also be matched.

2, when the n==1 should be a special sentence (PT array).

Code

1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6 Const intMAXN =1000000+Ten;7 8 intA[MAXN],B[MAXN],P[MAXN],T[MAXN],F[MAXN];9 intN,m,ans;Ten  One voidGet_fail () { Af[0]=f[1]=0; -      for(intI=1; i<n;i++) { -         intj=F[i]; the          while(J && P[i]!=p[j]) j=F[j]; -f[i+1]=p[i]==p[j]?j+1:0; -     } - } +  - intKMP () { + Get_fail (); A     intj=0, cnt=0; at      for(intI=0; i<m;i++) { -          while(J && T[i]!=p[j]) j=F[j]; -         if(T[i]==p[j]) J + +; -         if(j==n) cnt++,j=F[j]; -     } -     returnCNT; in } -  to intMain () { +scanf"%d%d",&n,&m); -      for(intI=0; i<n;i++) scanf ("%d",&a[i]); the      for(intI=0; i<m;i++) scanf ("%d",&b[i]); *     if(n==1) { $printf"%d\n", m*4);Panax Notoginseng         return 0; -     } the      +n--; m--; A      for(intI=0; i<n;i++) p[i]=a[i+1]-A[i]; the      for(intI=0; i<m;i++) t[i]=b[i+1]-B[i]; +Ans + =KMP (); -      $      for(intI=0; i<n;i++) p[i]=a[n-i]-a[n-i-1]; $Ans + =KMP (); -      -      for(intI=0; i<n;i++) p[i]=0; theAns + =2*KMP (); -     Wuyiprintf"%d\n", ans); the     return 0; -}

codevs3945 Perfect Rubbing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.