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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章