CodeForces 635B Island Puzzle

本文探讨了如何通过记录相对位置来解决字符串中特定字符在环形排列下的位置交换问题,详细介绍了算法实现过程和核心思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给定一个字符串你,其中为0的字符可以和相邻的交换位置(环形),问可以不可以变换成给定的字符串

思路:显然的交换并不会影响字符串的相对位置,所以记录一下相对位置就可以啦


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 200005
#define LL long long
int cas=1,T;
int a[maxn],b[maxn],t1,t2;
int main()
{
	int n;
	while (scanf("%d",&n)!=EOF)
	{
		for (int i = 0;i<n;i++)
		{
		   int x;
           scanf("%d",&x);
		   if (x==0)
			   continue;
		   a[t1++]=x;
		}
		for (int i = 0;i<n;i++)
		{
			int x;
			scanf("%d",&x);
			if (x==0)
				continue;
			b[t2++]=x;
		}
		int s = 0;
		for (int i = 0;i<t2;i++)
		{
			if (b[i]==a[0])
			{
				s=i;
				break;
			}
		}
		int flag = 0;
		for (int i = 0;i<n-1;i++)
			if (a[i]!=b[(i+s)%(n-1)])
			{
				flag=1;
				puts("NO");
				break;
			}
		if (!flag)
			puts("YES");
	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}


Description

A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.

The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.

Determine if it is possible for the islanders to arrange the statues in the desired order.

Input

The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands.

The second line contains n space-separated integers ai (0 ≤ ai ≤ n - 1) — the statue currently placed on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.

The third line contains n space-separated integers bi (0 ≤ bi ≤ n - 1) — the desired statues of the ith island. Once again, bi = 0indicates the island desires no statue. It is guaranteed that the bi are distinct.

Output

Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.

Sample Input

Input
3
1 0 2
2 0 1
Output
YES
Input
2
1 0
0 1
Output
YES
Input
4
1 2 3 0
0 3 2 1
Output
NO


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值