634A.Island Puzzle
来源:互联网 发布:mac用什么绘画软件 编辑:程序博客网 时间:2024/06/01 20:07
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.
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.
Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.
31 0 22 0 1
YES
21 00 1
YES
41 2 3 00 3 2 1
NO
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.
#!/usr/bin/python# -*- coding:utf-8 -*-def KMP(desired,currently): i=0 flag=True pos=currently.index(desired[0]) while(i<len(desired)): for j in range(pos,pos+len(currently)): if desired[i]==currently[j%len(currently)]: i=i+1 else: flag=False break if flag==False: break return flagn=int(raw_input())currently=map(int,raw_input().split(' '))desired=map(int,raw_input().split(' '))currently.remove(0)desired.remove(0)result=KMP(desired,currently)if result==True: print "YES"else: print"NO"
- 634A.Island Puzzle
- Codeforces 634A Island Puzzle 【水题】
- CodeForces 634A-Island Puzzle【找规律】
- CodeForces 634 A.Island Puzzle(水~)
- codeforces A. Orchestra B. Island Puzzle
- Codeforences B. Island Puzzle
- CodeForces 635B Island Puzzle
- A LONELY ISLAND
- CodeForces 635B Island Puzzle(小岛放雕塑,思维)
- Codeforces 635B Island Puzzle【最小表示法+思维】
- A hard puzzle
- extjs_css a litter puzzle
- 1097 A hard puzzle
- hdu1097 A hard puzzle
- 1097A hard puzzle
- 1097 A hard puzzle
- HDU - A hard puzzle
- A Boolean Array Puzzle
- Hadoop集群安装与配置
- C# 相对路径(整合)
- JSON和JSONP JSONP 实现跨域访问
- 【JavaScript】JS中String的split()活用
- Pro Git 中文版——Git 分支
- 634A.Island Puzzle
- Android中关于SimpleAdapter的简单理解
- YTU 1055: 输入字符串以及输出
- Pro Git中文版——服务器上的 Git
- STL与泛型编程<五>:Sets和Multisets
- 关于svn无法连接的问题
- 资产组合优化原理与实例 Portfolio Optimization
- Pro Git中文版——分布式 Git
- 633A.Ebony and Ivory