H: Clock Pictures(kmp匹配循环的字符串 )

来源:互联网 发布:rar解压王 for mac 编辑:程序博客网 时间:2024/05/29 16:12

问题 H: Clock Pictures

时间限制: 1 Sec  内存限制: 64 MB
提交: 32  解决: 9
[提交][状态][讨论版]

题目描述

You have two pictures of an unusual kind of clock. The clock has n hands, each having the same length and no kind of marking whatsoever. Also, the numbers on the clock are so faded that you can’t even tell anymore what direction is up in the picture. So the only thing that you see on the pictures, are n shades of the n hands, and nothing else. 
You’d like to know if both images might have been taken at exactly the same time of the day, possibly with the camera rotated at different angles.
Given the description of the two images, determine whether it is possible that these two pictures could be showing the same clock displaying the same time.

输入

The first line contains a single integer n (2 ≤ n ≤ 200 000), the number of hands on the clock.
Each of the next two lines contains n integers a i (0 ≤ a i < 360 000), representing the angles of the hands of the clock on one of the images, in thousandths of a degree. The first line represents the position of the hands on the first image, whereas the second line corresponds to the second image. The number a i denotes the angle between the recorded position of some hand and the upward direction in the image, measured clockwise. Angles of the same clock are distinct and are not given in any specific order.

输出

Output one line containing one word: possible if the clocks could be showing the same time,impossible otherwise.

样例输入

61 2 3 4 5 67 6 5 4 3 1

样例输出

impossible

输入n,代表钟表有n个指针,指针的形状一模一样,接着两行输入两个钟表的n个指针。

数的值代表当前指针距离标定指针的角度,但是顺序不是有序给出的,需要sort一下,然后就求出这些指针相邻间的距离,形成两个新的数组。

问题转换为判断这两个新的数组是否可以通过循环一一对应起来。

解决方法:

将其中一个字符串,复制为两份拼接起来,接着kmp就行了。

#include<bits/stdc++.h>using namespace std; const int N = 200020;int n;int a[N<<1],b[N];int aa[N<<1],bb[N];int fans = 0;void getnext(int *p,int *Next){    int j = 0,k = -1;    Next[0] = -1;    while(j < n-1){        if(k == -1 || p[j] == p[k]){            j++;k++;            Next[j] = k;        }        else{            k = Next[k];        }    }} void kmp(int *s,int *p){    int i = 0,j = 0;    int Next[N];    getnext(p,Next);    while(i < 2*n ){        if(j == -1 || s[i] == p[j]){            i++;            j++;        }        else{            j = Next[j];        }        if(j == n){            fans = 1;            return ;        }    } } int main(){        scanf("%d",&n);        for(int i = 0; i < n; ++i){            scanf("%d",&a[i]);        }        sort(a,a+n);        if(a[1] > a[0]){            for(int i=1;i<n;i++){                aa[i-1] = a[i] - a[i-1];            }            aa[n-1] = 360000 - (a[n-1] - a[0]);        }       // cout<<a[n]<<endl;        for(int i=0;i<n;i++){            aa[i+n] = aa[i];        }        for(int i = 0; i < n; ++i){            scanf("%d",&b[i]);        }        sort(b,b+n);        if(b[1] > b[0]){            for(int i=1;i<n;i++){                bb[i-1] = b[i] - b[i-1];            }            bb[n-1] = 360000 - (b[n-1] - b[0]);        }         kmp(aa,bb);        //cout<<fans<<endl;        if(fans)            puts("possible");        else            puts("impossible");    return 0;}


0 0