poj 1147 Binary codes
来源:互联网 发布:大数据人才缺口 编辑:程序博客网 时间:2024/04/29 00:35
Description
Consider a binary string (b1…bN) with N binary digits. Given such a string, the matrix of Figure 1 is formed from the rotated versions of the string.
b1b2…bN−1bNb2b3…bNb1…bN−1bN…bN−3bN−2bNb1…bN−2bN−1Figure 1. The rotated matrix
Then rows of the matrix are sorted in alphabetical order, where ‘0’ is before ‘1’. You are to write a program which, given the last column of the sorted matrix, finds the first row of the sorted matrix.
As an example, consider the string (00110). The sorted matrix is
0001100110011001000111000and the corresponding last column is (1 0 0 1 0). Given this last column your program should determine the first row, which is (0 0 0 1 1).
Input
The first line contains one integer N ≤ 3000, the number of binary digits in the binary string. The second line contains N integers, the binary digits in the last column from top to bottom.
Output
The first line contains N integers: the binary digits in the first row from left to right.
Sample Input
51 0 0 1 0
Sample Output
0 0 0 1 1
对由0,1组成的n个数,照题中的旋转,最后根据每行的字典序排序,组成n*n的矩阵,给出矩阵的最后1列,求矩阵
的第首行。
给出最后一列可以求出第0列,因为是按字典序排的,所以第0列肯定0在前,1在后,而第0列为0的相对位置在最后
1列不变,因为第0列都为0,又是按字典序排的。第0列为1也一样。根据第0列和最后一列就可以将对应关系求出,
也就是next数组。
例如样例的
0 0 0 1 1
0 0 1 1 0
0 1 1 0 0
1 0 0 0 1
1 1 0 0 0
next[ ]={1,2,4,0,3}
第0行第0列为0,第0行的第1列下次旋转后为第0列的第0行,所以第0行第1列为第0列的第1行为0,第1列的第0行
为第2列的第1行,为第0列的第2行,所以第0行的第2列为第0列的第2行为0,通过推理发现为next数组中元素递推
代码:
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int maxn=5000+100;int last[maxn];int first[maxn];int next[maxn];int main(){ int n; while(~scanf("%d",&n)) { for(int i=0;i<n;i++) { scanf("%d",&last[i]); first[i]=last[i]; } sort(first,first+n); int cur=0; int i; for(i=0;i<n;i++) { if(first[i]) break; while(last[cur]&&cur<n) cur++; next[i]=cur++; } cur=0; for(i=i;i<n;i++) { while(last[cur]==0&&cur<n) cur++; next[i]=cur++; } int k=0; for(int i=0;i<n-1;i++) { printf("%d ",first[k]); k=next[k]; } printf("%d\n",first[k]); } return 0;}
- poj 1147 Binary codes
- poj 1147 Binary codes
- POJ 1147 Binary Codes 杂题
- poj 杂题 - 1147 Binary codes
- POJ 1147 Binary codes 笔记
- poj 1147 Binary codes BWT压缩算法
- poj 1147 Binary codes bwt压缩算法
- poj 1147:Binary codes (置换群)
- POJ 1147 Binary codes (BWT算法 很巧妙)
- pku 1147 Binary codes 【规律】
- pku1147 Binary codes
- poj1147 Binary Codes
- POJ1147 Binary codes
- poj 1146 ID Codes
- POJ 1146 ID Codes
- poj 1146 ID Codes
- poj 1146 ID Codes
- POJ 1146 ID Codes
- 矩阵快速幂锦上添花小结
- 第九周项目5 从键盘输入20个整数,输出它们的和
- Uncover Security Design Flaws Using The STRIDE Approach
- Java Visual VM配置
- 倒置链表(数据结构)
- poj 1147 Binary codes
- 工控仪表控件Iocomp ActiveX系列教程:LPK文件授权
- (转)局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍
- 数学符号
- 多线程下不重复读取SQL Server 表的数据
- 横屏竖屏,视图旋转
- Hibernateproperties详解
- unity3d面试题摘选(全)
- [IOS]NSNotificationCenter消息通信机制介绍(KVO)