A. Binary Literature ( 思维构造 + 抽屉原理 )
题目链接
分析
我焯这个妙啊,抽屉原理;
题目要求两个2n的字符串合并成一个<3n的字符串;
也就是说这两个字符串一定要有>=n的公共子序列;
所以其实这个题的本质其实是找最长公共子序列;
最直接的想法就是,枚举任意两个子串,求他们的公共子序列,然后其他的字符插进去就行;(不会)
(但是找了一下, 好像没有O(n*logn)且能具体求出最长上升子序列的方法…有大佬知道的可以浇浇我)
另外一种解法:
首先观察到字符串只有0 1两种字符;
所以每个长度为2*n的字符串一定会有 cnt1 >= n 或者 cnt0 >= n;
所以根据抽屉原理,当有三个字符串时;一定有两个字符串cnt1 >= n 或者 cnt0 >= n;
也就是说一定有两个字符串的公共子序列都是0/1,且长度>=n,所以是一定满足的;
所以我们直接分成两种情况(最长公共子序列是0/1)去找公共子序列就行,然后其他字符插入;
代码
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<deque>
#include<stack>
#include<set>
#include <random>
#include<bitset>
// #include <unordered_map>
#include<math.h>
#include<string.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0);
using namespace std;
#define pb push_back
#define coutl cout<<"------------"<<endl;
#define fi first
#define se second
#define ire(x) scanf("%d",&x)
#define iire(a,b) scanf("%d %d",&a,&b)
#define lre(x) scanf("%lld",&x)
#define llre(a,b) scanf("%lld %lld",&a,&b)
#define dre(x) scanf("%lf",&x)
#define ddre(a,b) scanf("%lf %lf",&a,&b)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define endl "\n"
#define PI acos(-1.0)
//#define int long long
// #define double long double
// typedef __int128 ll;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, int> PDI;
typedef pair<ll, ll> PLL;
typedef pair<double, double> PDD;
typedef pair<double, pair<int, double> > PDID;
typedef pair<char, char> PCC;
typedef pair<char, pair<int, int> > PCII;
typedef pair<int, pair<int, int> > PIII;
typedef pair<int, pair<int, pair<int, int> > > PIIII;
typedef pair<ll, pair<int, int> > PLII;
const int maxn = 1e5 + 7;
const int N = 5e5 + 7;
const int M = 5e5 + 7;
const int mod = 998244353;
const int inv = mod - mod/2;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
const double eps = 1e-8;
ll gcd(ll a,ll b) {return b==0 ? a : gcd(b,a%b);}
ll lcm(ll a,ll b) {return a*b / gcd(a,b);}
ll qmi(ll a,ll b,ll p) {ll ans = 1; while(b) { if(b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans;}
int lowbit(int x) {return x & (-x);}
string ss[5];
int n;
//a b字符串构造出长度为n的,都是字符c的最长公共子序列
string sol(string a,string b,char c)
{
int l = 0;
int r = 0;
string ans;
for(int i=1;i<=n;i++)
{
//插入其他的字符
while(l < 2*n && a[l] != c) ans += a[l++];
while(r < 2*n && b[r] != c) ans += b[r++];
ans += c; //公共子序列的部分
l++; r++;
}
//插入剩下部分
while(l < 2*n) ans += a[l++];
while(r < 2*n) ans += b[r++];
return ans;
}
void solve()
{
cin>>n>>ss[1]>>ss[2]>>ss[3];
vector<string> v1,v2;
for(int i=1;i<=3;i++)
{
int cnt = 0; //最长公共子序列是0的个数
for(int j=0;j<2*n;j++) cnt += (ss[i][j] == '0');
if(cnt >= n) v1.push_back(ss[i]);
else v2.push_back(ss[i]);
}
string ans;
if(v1.size() > 1) ans = sol(v1[0],v1[1],'0');
else ans = sol(v2[0],v2[1],'1');
cout<<ans<<'\n';
int main()
{
IOS;
int t;
// ire(t);
cin>>t;
// t = 1;
while(t--)
{
solve();
}
return 0;
}