A bitwise XOR takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 11 if both bits are different (only one of them is 11 and the other is 00), but will be 00 if both bits are the same (both are 00 or both are 11). For example, the bitwise XOR of the patterns 01010101 and 11001100 is 10011001.
In this problem, you are given 33 binary strings of lengths 1010 digits such that all digits are either 00 or 11. You can swap any two digits in the same string infinitely, but you cannot swap two digits from two different strings.
Your task is to rearrange digits in the given strings in a way such that the bitwise XOR of the strings after rearranging their digits is as largest as possible. Can you?
Input
The first line contains an integer TT (1≤T≤2501≤T≤250) specifying the number of test cases.
Each test case consists of 33 lines each of which contains a binary string of length 1010 digits such that all digits are either 00 or 11.
Output
For each test, print a single line containing a binary string of length 1010representing the largest value of bitwise XOR that can be optioned by rearranging digits in each string.
A binary string xx is larger than a binary string yy if after converting both strings to the decimal representation, the decimal value of string xx is larger than the decimal value of string yy. For example, string "1100" is larger than string "0101" because its decimal value 1212, while the decimal value of string "0101" is 55.
Example
Input
2 0000101011 0001010101 0010010000 1000000010 0001000100 1001000000
Output
1111111111 1111110000
Note
In the first test case, you can rearrange the given strings as follow:
- "0000101011" →→ "0000111100"
- "0001010101" →→ "1111000000"
- "0010010000" →→ "0000000011"
#include<stdio.h> #include<string.h> #include<vector> int dp[20][20][20]; using namespace std; vector<int>p[16]; void init() { for(int i=0;i<1024;i++) { int m=i,num=0; while(m) { if(m&1) num++; m=m/2; } p[num].push_back(i); } } int main() { int T; scanf("%d",&T); init(); memset(dp,-1,sizeof(dp)); while(T--) { char a[20]; int n[10]; memset(n,0,sizeof(n)); for(int i=0;i<3;i++) { scanf("%s",a); for(int j=0;j<10;j++) { if(a[j]=='1') n[i+1]++; } } int ans=0; if(dp[n[1]][n[2]][n[3]]!=-1) ans=dp[n[1]][n[2]][n[3]];//优化一下,不优化会超时 else { for(int i=0;i<p[n[1]].size();i++) { for(int j=0;j<p[n[2]].size();j++) { for(int k=0;k<p[n[3]].size();k++) { int a=p[n[1]][i]; int b=p[n[2]][j]; int c=p[n[3]][k]; ans=max(ans,a^b^c); } } } dp[n[1]][n[2]][n[3]]=ans; } int d[20]; memset(d,0,sizeof(d)); int i=9; while(ans) { if(ans&1)d[i--]=1; else d[i--]=0; ans=ans/2; } for(int i=0;i<=9;i++) { printf("%d",d[i]); } printf("\n"); } return 0; }