D - XOR Permutations

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"

题解:10位二进制最大表示1023,先预处理0到1023之间的数,以二进制中1的个数为第一维放入vector,开启三维dp数组(其实也不算是dp了)记录最优值,状态是一定的,只要算出来其中的状态即可直接输出,否则需要计算这种状态并且将其记录dp中,三重循环求得最大异或值即可。

#include<bits/stdc++.h>
using namespace std;
vector<int>p[15];
int dp[15][15][15];//dp[i][j][k]代表三个二进制数中1的个数分别为i,j,k的最大值
int num[15];
void Init()
{
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<1024;i++)//10位数最大为1023
    {
        int x=i,sum=0;
        while(x)
        {
            if(x&1)
                sum++;//计算二进制中1的个数
            x>>=1;
        }
        p[sum].push_back(i);//该数i中二进制中1的个数为sum个,放入sum的vector
    }
}
void print(int x)
{
    stack<int>S;//二进制放入栈中先进后出
    while(x)
    {
        S.push(x&1);
        x>>=1;
    }
    int n=S.size();
    while(!S.empty())
    {
        cout<<S.top();
        S.pop();
    }
    for(int i=n+1;i<=10;i++)//这里要不足位补0
        cout<<"0";
    cout<<endl;
}

int main()
{
    Init();
    int t;
    cin>>t;
    while(t--)
    {
        memset(num,0,sizeof(num));
        for(int i=1;i<=3;i++)
        {
            string s;
            cin>>s;
            for(int j=0;j<s.size();j++)
            {
                if(s[j]=='1')
                    num[i]++;
            }
        }
        if(dp[num[1]][num[2]][num[3]]!=-1)
        {
            print(dp[num[1]][num[2]][num[3]]);//该状态已经算出来了,打印该数
            continue;
        }
        int ans=0;
        for(int i=0;i<p[num[1]].size();i++)
        {
            for(int j=0;j<p[num[2]].size();j++)
            {
                for(int k=0;k<p[num[3]].size();k++)
                {
                    int u=p[num[1]][i];
                    int v=p[num[2]][j];
                    int w=p[num[3]][k];
                    ans=max(ans,u^v^w);//循环求得最大异或值
                }
            }
        }
        dp[num[1]][num[2]][num[3]]=ans;//更新dp数组
        print(ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43824158/article/details/89143556
今日推荐