codeforces B. Island Puzzle
2016-02-29 23:51
344 查看
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
output
input
output
input
output
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);
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);
#include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<math.h> #include<queue> #include<map> using namespace std; typedef long long LL; void next(int n); int aa[200005]; int a[400005]; int bb[200005]; int f[200005]; int nex[200005]; int main(void) { int i,j,k,p,q; cin>>k;int cnt=0; for(i=0;i<k;i++) { cin>>p; if(p!=0) {cnt++;a[cnt]=aa[cnt]=p;} } for(i=cnt+1;i<=2*cnt;i++) { a[i]=aa[i-cnt]; } int ans=0; for(i=0;i<k;i++) { cin>>p; if(p!=0) { ans++; bb[ans]=p; } } next(ans); j=0;int flag=0; for(i=1;i<=2*cnt;i++) { while(j>0&&bb[j+1]!=a[i]) { j=nex[j]; } if(bb[j+1]==a[i]) { j++; } if(j==ans) {flag=1; break; } }if(flag) { printf("YES\n"); } else printf("NO\n"); return 0; } void next(int n) { int i,j; nex[0]=0; nex[1]=0;j=0; for(i=2;i<=n;i++) { while(bb[j+1]!=bb[i]&&j>0) { j=nex[j]; } if(bb[j+1]==bb[i]) { j++; } nex[i]=j; } }
相关文章推荐
- 团与饿了么的B2B大战,背后的公关阴谋论
- 字符格式的数字转化为整型数字
- Android源码树中C代码的编译
- GitLab 安装配置笔记(转)
- HTTPS与强制门户
- android常见异常总结
- linux打包压缩命令汇总
- C语言关于利用sscanf实现字符串相加减
- C++的可移植性和跨平台开发
- druid 采用mbean监控
- 冒泡排序
- java静态方法与实例方法的区别
- 选择排序
- Java中this关键字的几种用法
- awk
- 程序员修炼之道:从小工到专家
- PHP之类特性
- 详解JDK 5 Annotation 注解之@Target的用法介绍
- TO-DO List
- poj2342&&hdu1520(树形dp)