B. Island Puzzle
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
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
Note

In the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.

In the second sample, the islanders can simply move statue 1 from island 1 to island 2.

In the third sample, no sequence of movements results in the desired position.

思路:KMP

其实0可以省略看成没有,去0后不改变原来的顺序,其实只要适当移动0可以移到任意位置,所以把0全移到最前这样就能看成没有02020---00022。然后把去0后的原窜复制一遍放在后面,然后下面的窜去0后对上串KMP即可。O(n);

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<math.h>
6 #include<queue>
7 #include<map>
8 using namespace std;
9 typedef long long LL;
10 void next(int n);
11 int aa[200005];
12 int a[400005];
13 int bb[200005];
14 int f[200005];
15 int nex[200005];
16 int main(void)
17 {
18 int i,j,k,p,q;
19 cin>>k;int cnt=0;
20 for(i=0;i<k;i++)
21 {
22 cin>>p;
23 if(p!=0)
24 {cnt++;a[cnt]=aa[cnt]=p;}
25 }
26 for(i=cnt+1;i<=2*cnt;i++)
27 {
28 a[i]=aa[i-cnt];
29 }
30 int ans=0;
31 for(i=0;i<k;i++)
32 {
33 cin>>p;
34 if(p!=0)
35 { ans++;
36 bb[ans]=p;
37
38 }
39 }
40 next(ans);
41 j=0;int flag=0;
42 for(i=1;i<=2*cnt;i++)
43 {
44 while(j>0&&bb[j+1]!=a[i])
45 {
46 j=nex[j];
47 }
48 if(bb[j+1]==a[i])
49 {
50 j++;
51 }
52 if(j==ans)
53 {flag=1;
54 break;
55 }
56 }if(flag)
57 {
58 printf("YES\n");
59 }
60 else printf("NO\n");
61 return 0;
62 }
63 void next(int n)
64 {
65 int i,j;
66 nex[0]=0;
67 nex[1]=0;j=0;
68 for(i=2;i<=n;i++)
69 {
70 while(bb[j+1]!=bb[i]&&j>0)
71 {
72 j=nex[j];
73 }
74 if(bb[j+1]==bb[i])
75 {
76 j++;
77 }
78 nex[i]=j;
79 }
80 }

codeforces B. Island Puzzle的更多相关文章

  1. codeforces A. Orchestra B. Island Puzzle

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. codeforce B Island Puzzle

    B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. CF 634A Island Puzzle

    A. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. CodeForces 303B Rectangle Puzzle II

    题意: 给定一个靠着坐标轴长为n,宽为m的矩形和 矩形中的一个点A,求在这个矩形内部一个 长宽比为a/b的小矩形,使这个小矩形的长宽尽量大使点A在小矩形内部,并且点A尽量靠近小矩形的中心 CF的思维题 ...

  5. 冬训 day2

    模拟枚举... A - New Year and Buggy Bot(http://codeforces.com/problemset/problem/908/B) 暴力枚举即可,但是直接手动暴力会非 ...

  6. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)

    暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...

  7. 贪心/构造/DP 杂题选做Ⅱ

    由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...

  8. Codeforces Round #385 (Div. 2) B - Hongcow Solves A Puzzle 暴力

    B - Hongcow Solves A Puzzle 题目连接: http://codeforces.com/contest/745/problem/B Description Hongcow li ...

  9. Codeforces Round #172 (Div. 2) C. Rectangle Puzzle 数学题几何

    C. Rectangle Puzzle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/281/p ...

随机推荐

  1. 类成员函数调用delete this会发生什么呢?

    有如下代码 class myClass { public: myClass(){}; ~myClass(){}; void foo() { delete this; } }; int main() { ...

  2. When does compiler create default and copy constructors in C++?

    In C++, compiler creates a default constructor if we don't define our own constructor (See this). Co ...

  3. 【Linux】【Commands】基础概念及常用基础命令

    命令的语法通用格式: ------------------------------------------------ #COMMAND OPTIONS ARGUMENTS 发起命令:请求内核将某个二 ...

  4. 【Java 8】函数式接口(一)—— Functional Interface简介

    什么是函数式接口(Functional Interface) 其实之前在讲Lambda表达式的时候提到过,所谓的函数式接口,当然首先是一个接口,然后就是在这个接口里面只能有一个抽象方法. 这种类型的接 ...

  5. 【Java 基础】Java 根据Class获取对象实例

    Spring在代码中获取bean的几种方式 方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法 ...

  6. 通过Jedis操作Redis

    package com.yh; import org.junit.After; import org.junit.Before; import org.junit.Test; import redis ...

  7. 使用Stream方式处理集合元素

    package com.itheima.demo03.Stream;import java.util.ArrayList;import java.util.stream.Stream;/** * @a ...

  8. 【C#】【MySQL】C#连接MySQL数据库(二)解析

    C# MySQL 实现简单登录验证 后端代码解析 Visual Studio中使用MySQL的环境配置 下文所有到的代码(前端后端) 请查阅这篇博文 C#连接MySQL数据库(一)代码 获取前端数据 ...

  9. 使用RabbitMQ搭建MQTT服务

    由于近期公司需要搭建一套物联网采集环境,底层设备采用MQTT协议传输数据.服务器环境为linux,考虑到现有环境已经有RabbitMQ环境,Rabbit是基于AMQP协议开发的一套高效的消息传输队列. ...

  10. 对于React各种状态管理器的解读

    首先我们要先知道什么是状态管理器,这玩意是干啥的? 当我们在多个页面中使用到了相同的属性时就可以用到状态管理器,将这些状态存到外部的一个单独的文件中,不管在什么时候想使用都可以很方便的获取. reac ...