The North American Invitational Programming Contest 2018 H. Recovery

Consider an n \times mn×m matrix of ones and zeros. For example, this 4 \times 44×4:

\displaystyle \begin{matrix} 1 & 1 & 1 & 1 \\ 0 & 1 & 1 & 1 \\ 0 & 1 & 1 & 1 \\ 0 & 1 & 1 & 0 \end{matrix}1000​1111​1111​1110​

We can compute even parity for each row, and each column. In this case, the row parities are [0, 1, 1, 0][0,1,1,0] and the column parities are [1, 0, 0, 1][1,0,0,1] (the parity is 11 if there is an odd number of 11s in the row or column, 00 if the number of 11s is even). Note that the top row is row 11, the bottom row is row nn, the leftmost column is column 11, and the rightmost column is column mm.

Suppose we lost the original matrix, and only have the row and column parities. Can we recover the original matrix? Unfortunately, we cannot uniquely recover the original matrix, but with some constraints, we can uniquely recover a matrix that fits the bill. Firstly, the recovered matrix must contain as many 11’s as possible. Secondly, of all possible recovered matrices with the most 11’s, use the one which has the smallest binary value when you start with row 11, concatenate row 22 to the end of row 11, then append row 33, row 44, and so on.

Input Format

Each input will consist of a single test case.

Note that your program may be run multiple times on different inputs.

Each test case will consist of exactly two lines.

The first line will contain a string R (1 \le |R| \le 50)R(1≤∣R∣≤50), consisting only of the characters 00 and 11. These are the row parities, in order.

The second line will contain a string C (1 \le |C| \le 50)C(1≤∣C∣≤50), consisting only of the characters 00 and 11. These are the column parities, in order.

Output Format

If it is possible to recover the original matrix with the given constraints, then output the matrix as |R|∣R∣ lines of exactly |C|∣C∣ characters, consisting only of 00’s and 11’s. If it is not possible to recover the original matrix, output -1−1.

样例输入1

0110
1001

样例输出1

1111
0111
1110
1111

样例输入2

0
1

样例输出2

-1

样例输入3

11
0110

样例输出3

1011
1101

题目来源

The North American Invitational Programming Contest 2018

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
using namespace std;
#define ll long long
#define N 60
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define gepp(i,a,b) for(int i=a;i>=b;i--)
#define gep1(i,a,b) for(ll i=a;i<=b;i++)
#define gepp1(i,a,b) for(ll i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
char s1[N],s2[N];
int a[N],b[N];
char s[N][N];
int main()
{
scanf("%s%s",s1,s2);
int l=strlen(s1);
int r=strlen(s2);
int x=l%,y=r%;
int cnt1=,cnt2=;
gep(i,,l-){
int ii=s1[i]-'';
if(ii%!=y){
a[cnt1++]=i;
}
}
gep(i,,r-){
int ii=s2[i]-'';
if(ii%!=x){
b[cnt2++]=i;
}
}
if((cnt1+cnt2)&){//必须为偶数
printf("-1\n");
return ;
}
while(cnt1<cnt2) a[cnt1++]=;
while(cnt2<cnt1) b[cnt2++]=;
sort(a,a+cnt1);sort(b,b+cnt2);//贪心
gep(i,,l-){
gep(j,,r-){
s[i][j]='';
}
}
gep(i,,cnt1-){
s[a[i]][b[i]]='';//这些点必须为0
}
gep(i,,l-){
printf("%s\n",s[i]);
}
return ;
}
上一篇:The North American Invitational Programming Contest 2018 D. Missing Gnomes


下一篇:The North American Invitational Programming Contest 2018 E. Prefix Free Code