CF 2014 Nordic Collegiate Programming Contest H.Clock Pictures

http://codeforces.com/gym/100502

H.Clock Pictures

You have two pictures of an unusual kind of clock. The clock hasn 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, aren shades of then 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 integern (2n200000), the number of hands on the clock.
Each of the next two lines containsn integersa i(0ai<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 numbera idenotes 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,
impossibleotherwise.

Sample Input 1
6
1 2 3 4 5 6
7 6 5 4 3 1


Sample Output 1
impossible

Sample Input 2
2
0 270000
180000 270000


Sample Output 2
possible

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


Sample Output 3
impossible

解析

注意时钟角度输入是无序的,所以读入后排个序。然后求DertaDegree[i]。表示这个指针(顺时针方向)到下一个最近指针转过的角度。然后两个时钟的DertaDegree[i]进行一次字符串匹配即可。

第一次写KMP,所以注释比较多。

介绍两篇KMP写得不错的blog
Matrix67 http://www.matrix67.com/blog/archives/115

v_JULY_v http://blog.csdn.net/v_july_v/article/details/7041827

#include<cstdio>
#include<algorithm>

using namespace std;

int N,DDeg1[200100],DDeg2[400100],Deg1[200100],Deg2[200100],Pre[200100];

void readdata()
{
	for(int i=1;i<=N;i++)scanf("%d",&Deg1[i]);
	sort(Deg1+1,Deg1+1+N);
	for(int i=1;i<N;i++) DDeg1[i]=Deg1[i+1]-Deg1[i]; DDeg1[N]=360000-Deg1[N]+Deg1[1];

	for(int i=1;i<=N;i++)scanf("%d",&Deg2[i]);
	sort(Deg2+1,Deg2+1+N);
	for(int i=1;i<N;i++) DDeg2[i]=Deg2[i+1]-Deg2[i]; DDeg2[N]=360000-Deg2[N]+Deg2[1];
	//在DDeg2中寻找子串DDeg1
	for(int i=N+1;i<=2*N;i++) DDeg2[i]=DDeg2[i-N];	
}

void kmp()
{	
	//DDeg1为模板串 DDeg2为文本串
	//对于Pre[i]我们必须保证满足这样的性质:字符串[1,Pre[i]]是字符串[1,i]的后缀子串
	
	//kmp prework
	int j=0; Pre[1]=0;
	for(int i=2;i<=N;i++)
	{
		//j里面保存的是上一格失配后应该跳回的地方
		while(j>0 && DDeg1[j+1]!=DDeg1[i]) j=Pre[j];
		//这里有两种情况跳出了循环
		//1.j==0 那么Pre[i]=0;
		//2.DDeg1[j+1]==DDeg1[i] 那么Pre[i]=j+1;
		if(DDeg1[j+1]==DDeg1[i]) Pre[i]=++j;
		else Pre[i]=0;
	}

	j=0;
	bool flag=0;
	for(int i=1;i<=2*N;i++)
	{
		while(j>0 && DDeg1[j+1]!=DDeg2[i]) j=Pre[j];
		if(DDeg1[j+1]==DDeg2[i]) j++;
		if(j==N) 
		{
			flag=1;
			break; //如果要多次找子串还可以去掉break;
			j=Pre[j]; //多次找子串时,使算法进行下去
		}
	}

	if(flag) printf("possible");
	else printf("impossible");
}

int main()
{
	while(scanf("%d",&N)==1)
	{
		readdata();
		kmp();
	}

	return 0;	
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值