Kattis clockpictures KMP

题目描述:

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.
Task

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.
Input

The first line contains a single integer n
(2≤n≤200000

), the number of hands on the clock.

Each of the next two lines contains n
integers ai (0≤ai<360000), 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 ai

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

Output one line containing one word: possible if the clocks could be showing the same time, impossible otherwise.
\includegraphics[width=0.45\textwidth ]{sample2}
Figure 1: Sample input 2
Sample Input 1 Sample Output 1

6
1 2 3 4 5 6
7 6 5 4 3 1

impossible

Sample Input 2 Sample Output 2

2
0 270000
180000 270000

possible

Sample Input 3 Sample Output 3

7
140 130 110 120 125 100 105
235 205 215 220 225 200 240

impossible

题解:

题目的意思是2个表盘上有些一模一样的指针,在看这个表的时候可以把表盘旋转任意角度,问这两个表盘能不能存在一个旋转的角度能够一摸一样。
做法是分别把两个表盘的相邻指针的夹角做出来,分别是a1[ ]和b1[ ],要做一圈的,所以最后一个分别是360000+a[0]-a[n-1],360000+b[0]-b[n-1]。
然后再把a[ ]做成双倍的长度,最后b[ ]在a[ ]上做个KMP。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=200005;
int a[maxn],b[maxn],a1[maxn],b1[maxn];
int n;
void getnext(int *p,int next[])
{
    int pLen=n;
    next[0]=-1;
    int k=-1;
    int j=0;
    while(j<pLen-1)
    {
        if(k==-1||p[j]==p[k])
        {
            k++;j++;
            if (p[j]!=p[k]) next[j]=k;     
            else next[j]=k;
        }
        else k=next[k];
    }
}
bool kmpsearch(int *s,int *p)
{
    int i=0,j=0;
    int sLen=2*n;
    int pLen=n;
    int next[maxn];
    getnext(p,next);
    while(i<sLen&&j<pLen)
    {
        if(j==-1||s[i]==p[j]) 
        {
            i++;
            j++;
        }   
        else j=next[j];
    }
    if(j==pLen) return 1;
    else return 0;
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        for(int i=0;i<n;i++) scanf("%d",&b[i]);
        sort(a,a+n);
        sort(b,b+n);
        for(int i=1;i<n;i++) a1[i-1]=a[i]-a[i-1];
        for(int i=1;i<n;i++) b1[i-1]=b[i]-b[i-1];
        a1[n-1]=360000+a[0]-a[n-1];
        b1[n-1]=360000+b[0]-b[n-1];
        for(int i=0;i<n;i++) a1[i+n]=a1[i];
        if(kmpsearch(a1,b1)) puts("possible"); 
        else puts("impossible"); 
    } 
    return 0;
}
    原文作者:KMP算法
    原文地址: https://blog.csdn.net/Apel_dey/article/details/79429537
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞