KMP CSU1581 (NCPC2014)

6 篇文章 0 订阅

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 <cstdio>
#include <algorithm>
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<n;++i){
		int j=Next[i];
		while(j&&s[i]!=s[j]) j=Next[j];
		if(s[i]==s[j]) Next[i+1]=j+1;
		else Next[i+1]=0;
	}
}

void Find(int* ss,int* s,int* Next)
{
	get_Next(s,Next);
	int j=0;
	for(int i=0;i<2*n;++i){
		while(j&&s[j]!=ss[i]) j=Next[j];
		if(s[j]==ss[i]) ++j;
		if(j==n){
			flag=1;
			return;
		}
	}
}

int main()
{
	while(~scanf("%d",&n)){
		flag=0;
		for(int i=0;i<n;++i){
			scanf("%d",&a[i]);
		}
		sort(a,a+n);
		da[0]=da[n]=(a[0]-a[n-1]+360000)%360000;
		for(int i=1;i<n;++i){
			da[i]=da[i+n]=a[i]-a[i-1];
		}
		for(int i=0;i<n;++i){
			scanf("%d",&b[i]);
		}
		sort(b,b+n);
		db[0]=(b[0]-b[n-1]+360000)%360000;
		for(int i=1;i<n;++i){
			db[i]=b[i]-b[i-1];
		}
		Find(da,db,Next);
		if(flag)
			printf("possible\n");
		else
			printf("impossible\n");
	}

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值