题意:给定三个长度为10由0,1组成的字符串,规定可以在同一字符串中任意交换两个位置的数,求经过操作后三个字符串的异或值最大是多少。
思路:由异或的性质,不同为1,相同为0,故对于三个字符串来说,想要某一位置上是1,只有两种情况:1,1,1或1,0,0。由于可以任意互换位置,我们只需要确定三个字符串0和1分别的数量并组合上述的两种情况即可。由于要最多数量的1,我们以1,0,0这种情况优先。
#include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ll long long
struct node
{
int n1,n2;//n1是1的个数,n2是0的个数
node()
{
n1=n2=0;
}
};
bool cmp(node a,node b)
{
return a.n1>b.n1;
}
int main()
{
IOS;
int t;
cin >> t;
while (t--)
{
string a[3];
cin >> a[0] >> a[1] >> a[2];
node b[4];
for(int i=0;i<3;i++)
{
for(int j=0;j<a[i].size();j++)
{
if(a[i][j]=='1')
b[i].n1++;
else
b[i].n2++;
}
}
for(int i=0;i<10;i++)
{
sort(b,b+3,cmp);
if(b[0].n1>0&&b[1].n2>0&&b[2].n2>0)
{
cout<<1;
b[0].n1--;
b[1].n2--;
b[2].n2--;
}
else if(b[0].n1>0&&b[1].n1>0&&b[2].n1>0)
{
cout<<1;
b[0].n1--;
b[1].n1--;
b[2].n1--;
}
else
cout<<0;
}
cout<<endl;
}
return 0;
}