H - Clock Pictures(kmp)

题意:给出两个表盘中各个指针与当前表盘正上方向的夹角。求出两个表盘是否是经过一定程度旋转的相同表盘。

思路:若是相同表盘,则指针的相对位置一定。即每个指针之间的差是相同的。故我们可以以某一表盘为标准,计算出相邻指针间的间隔距离,扩展到两倍,作为主串。另一表盘的指针间距作为匹配串使用kmp算法,看是否在主串中出现,若出现,则说明可以旋转某一角度使使两表盘重合。

代码如下:

#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cstdio>
#define INF 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define inf 0x3f3f3f3f3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for (auto i = a; i <= b; ++i)
#define bep(i, a, b) for (auto i = a; i >= b; --i)
#define lowbit(x) x &(-x)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
#define X1 first
#define Y1 second
#define IOS                      \
	ios::sync_with_stdio(false); \
	cin.tie(0);                  \
	cout.tie(0);
const int MO = 1e9 + 10;
const int NN = 1e8 + 10;
const int MM = 1e7 + 10;
const int N = 1e6 + 10;
const int M = 2e5 + 10;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int dxy[][2] = {{0, 1}, {1, 0}, {1, 1}, {-1, 1}};
using namespace std;

int Next[MM];
int a[MM];
int b[MM];
int cha1[MM];
int cha2[MM];
int n;


void getNext()
{
	int i, j;
	i = 0;
	Next[0] = j = -1;
	while (i < n)
	{
		if (j == -1 || cha1[i] == cha1[j])
		{
			Next[i + 1] = j + 1;
			if (cha1[j + 1] == cha1[i + 1])
				Next[i + 1] = Next[j + 1];
			i++;
			j = j + 1;
		}
		else
			j = Next[j];
	}
} 

bool kmp(){
    int i,j;
    i=j=0;
    while(i<2*n && j<n){
        if(j==-1 || cha1[i]==cha2[j]){
            i++;
            j++;
        }
        else j=Next[j];
    }
    if(j==n) return true;
    else return false;
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i] %= 360;
	}
	for (int i = 0; i < n; i++)
	{
		cin >> b[i];
		b[i] %= 360;
	}
	sort(a, a + n);
	sort(b, b + n);
	for (int i = 0; i < n - 1; i++)
	{
		cha1[i] = a[i + 1] - a[i];
		cha2[i] = b[i + 1] - b[i];
	}
	cha1[n - 1] = 360 - (a[n - 1] - a[0]);
	cha2[n - 1] = 360 - (b[n - 1] - b[0]);
	for (int i = n; i <= 2 * n - 1; i++)
	{
		cha1[i] = cha1[i - n];
	}
	getNext();
	if (kmp())
		cout << "possible" << endl;
	else
		cout << "impossible" << endl;

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值