您的位置:首页 > 其它

文章标题 coderforces 761B : Dasha and friends(KMP)

2017-02-02 15:16 423 查看

Dasha and friends

Running with barriers on the circle track is very popular in the country where Dasha lives, so no wonder that on her way to classes she saw the following situation:

The track is the circle with length L, in distinct points of which there are n barriers. Athlete always run the track in counterclockwise direction if you look on him from above. All barriers are located at integer distance from each other along the track.

Her friends the parrot Kefa and the leopard Sasha participated in competitions and each of them ran one lap. Each of the friends started from some integral point on the track. Both friends wrote the distance from their start along the track to each of the n barriers. Thus, each of them wrote n integers in the ascending order, each of them was between 0 and L - 1, inclusively.

Consider an example. Let L = 8, blue points are barriers, and green points are Kefa’s start (A) and Sasha’s start (B). Then Kefa writes down the sequence [2, 4, 6], and Sasha writes down [1, 5, 7].

There are several tracks in the country, all of them have same length and same number of barriers, but the positions of the barriers can differ among different tracks. Now Dasha is interested if it is possible that Kefa and Sasha ran the same track or they participated on different tracks.

Write the program which will check that Kefa’s and Sasha’s tracks coincide (it means that one can be obtained from the other by changing the start position). Note that they always run the track in one direction — counterclockwise, if you look on a track from above.

Input

The first line contains two integers n and L (1 ≤ n ≤ 50, n ≤ L ≤ 100) — the number of barriers on a track and its length.

The second line contains n distinct integers in the ascending order — the distance from Kefa’s start to each barrier in the order of its appearance. All integers are in the range from 0 to L - 1 inclusively.

The second line contains n distinct integers in the ascending order — the distance from Sasha’s start to each barrier in the order of its overcoming. All integers are in the range from 0 to L - 1 inclusively.

Output

Print “YES” (without quotes), if Kefa and Sasha ran the coinciding tracks (it means that the position of all barriers coincides, if they start running from the same points on the track). Otherwise print “NO” (without quotes).

Examples

input

3 8

2 4 6

1 5 7

output

YES

input

4 9

2 3 5 8

0 1 3 6

output

YES

input

2 4

1 3

1 2

output

NO

Note

The first test is analyzed in the statement.

题意:有一个周长为L的圆,圆上有n个障碍物,现在有两个人在圆上,现在给出两个人往逆时针方向上与n个障碍物的距离,现在要求出这两个人所在的圆是不是一样的。

分析:想确定所在的圆是不是一样的,可以先求一下两个圆中,相邻的两个障碍物的距离的序列,然后比较一下看是否有相同的序列串,可以用KMP求一下是否有相同的串。

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
int l,n;
int fail[205];//next指针
int text[205];//文本串
int match[205];
int s[105];//模式串
int a[105],b[105];
void getfail(){//求指针
memset (fail,0,sizeof (fail));
fail[0]=-1;
for (int i=1;i<=n;i++){
int p=fail[i-1];
while (p>=0&&s[p+1]!=s[i]) p=fail[p];
if (p<0)fail[i]=0;
else fail[i]=p+1;
}
}
int getmarch(){
memset(match,0,sizeof (match));
int ans=0;
for (int i=1;i<=2*n;i++){
int p=match[i-1];
while (p>0&&s[p+1]!=text[i])p=fail[p];
if (p<0)match[i]=0;
else match[i]=p+1;
if (match[i]==n) return 1;//如果为n,则成功
}
return 0;
}
int main ()
{
while (cin>>n>>l){
for (int i=1;i<=n;i++){//输入
cin>>a[i];
}
for (int i=1;i<=n;i++){
cin>>b[i];
}
for (int i=1;i<=n-1;i++){//第一个人为模式串,
s[i]=a[i+1]-a[i];
}
s
=(l-a
)+a[1];//第n个障碍与第一个的距离
for (int i=1;i<=n-1;i++){//第二个人为文本串,在后面在加相同的一串
text[i]=text[i+n]=b[i+1]-b[i];
}
text
=text[2*n]=(l-b
)+b[1];//第一个与第n个障碍的距离
getfail();//求指针
int flag=getmarch();//看是否成功
if (flag==0){
cout<<"NO"<<endl;
}
else cout<<"YES"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: