Binary codes
| Time limit:1000 ms |
|
Memory limit:65536 K |
| Total submissions:5647 |
|
Accepted:2201 |
Description
Consider a binary string (b1... BN)NBinary digits. Given such a string, the matrix of Figure 1 is formed from the rotated versions of the string.
| B1 |
B2 |
... |
BN? 1 |
BN |
| B2 |
B3 |
... |
BN |
B1 |
| ... |
| BN? 1 |
BN |
... |
BN? 3 |
BN? 2 |
| BN |
B1 |
... |
BN? 2 |
BN? 1 |
Figure 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
| 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 |
And the corresponding last column is (1 0 0 1 0). Given this last column your program shoshould determine the first row, which is (0 0 0 1 ).
Input
The first line contains one integerN≤ 3000, the number of binary digits in the binary string. The second line containsNIntegers, the binary digits in the last column from top to bottom.
Output
The first line containsNIntegers: 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
For the number of N consisting of 0, 1, according to the rotation in the question, and finally sorted according to the Lexicographic Order of each row, form the N * n matrix, give the last column of the Matrix, find the Matrix
The first line.
Given the last column, we can find 0th columns. Because the column is in the lexicographically ordered order, the first column of 0th must be 0, the first column is 1, and the second column is 0.
1 columns remain unchanged, because 0th columns are all 0 and are alphabetically arranged. The same applies to column 1 in column 0th. You can find the corresponding relationship between the 0th column and the last column,
That is, the next array.
For example
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}
0th rows, 0th columns, and 0th columns of 1st rows are The 0th rows of 0th columns after the next rotation. Therefore, 0th columns of 1st rows, 0th columns, 1st rows of 1st columns, and 0th rows of Columns
It is the 2nd rows of the 1st columns, which is the 0th rows of the 2nd columns. Therefore, the 0th behavior of the 2nd columns of the 0th rows is 0. It is found by inference that the element in the next array is recursive.
Code:
#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