KMP CSU1581 (NCPC2014)

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.

样例输入

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

样例输出

impossible

 

首先这个题的题意要好好理解:表有很多一样的指针,没有刻度,给出两个表盘,及表盘上的指针与12点方向(但那个位置不一定是12了现在)的角度(0~360000)(注意没有固定给出顺序),问两个表盘是否有可能是表示同一时间(一个表经旋转后与另一个表上指针位置相同)。

完全想不到的正解思路(hiahiahia):

对每个表先进行处理:指针角度排序,求出差值;

匹配差值如果一个一个去匹配,时间复杂度绝对会爆(毕竟200000的数据),所以这时就有了一个巧妙的方法:将a(某一)串倍长,用b做模式串去2a中匹配,即kmp~真的巧妙鸭~

 

#include 
#include 
using namespace std;

const int maxn=200020;

int n;
int Next[maxn];
int a[maxn],b[maxn],da[maxn*2],db[maxn];
int flag;

void get_Next(int* s,int* Next)
{
	Next[0]=Next[1]=0;
	for(int i=1;i

 

你可能感兴趣的:(ACM,KMP)