Codeforces761B. Dasha and friends (KMP)

B. Dasha and friends
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
分析
以第一组样例为例
3 8
2 4 6
1 5 7
把点转换成两点的间隔,
2 2 4(4-2, 6-4,(8-2-2))
4 2(5-1,7-5)
把第一个人的间隔当主串(主串要复制一次比如224,要复制成224224,因为有环),第二人的间隔(42)当子串,KMP成功则YES,否则NO

#include<iostream>
#include<math.h>
using namespace std;
int Next[1000];
int num[100];


void get_next(const string sub, int *next)  
{  
    int len=sub.length();  
    int i,k;  
    next[0]=k=-1;  
    for (i=0; i<len;)  
    {  
        if (k==-1 || sub[i]==sub[k])  
        {  
            k++; i++;  
            if (sub[k]!=sub[i]) next[i]=k;  
            else next[i]=next[k];     
        }  
        else k=next[k];  
    }  
}  

int KMP(const string str, const string sub, const int *next)              
{  
    int i,j;  
    int len1=str.length();  
    int len2=sub.length();  
    for (i=0, j=0; i<len1 && j<len2;)  
    {  
        if (j==-1 || str[i]==sub[j])  
        {  
            i++; j++;  
        }  
        else   
        {  
            j=next[j];  
        }  
    }  
    if (j==len2) return i-len2;  
    return -1;      
}  
int main(){
    int n,l;
    cin>>n>>l;
    string s,t,p,q;
    int sum=0;
    for(int i=0;i<n;i++)
        cin>>num[i];
    for(int i=1;i<n;i++)
        s+=num[i]-num[i-1]+'0',sum+=num[i]-num[i-1];

    s+=l-sum+'0';
    for(int i=0;i<n;i++)
        cin>>num[i];
    for(int i=1;i<n;i++)
        t+=num[i]-num[i-1]+'0';
    s+=s;
    get_next(t.c_str(),Next);
    int ret=KMP(s.c_str(),t.c_str(),Next);
    if(ret >=0)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值