D. Binary Literature_Codeforces Round #715 (Div. 2)

Posted 出尘呢

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D. Binary Literature_Codeforces Round #715 (Div. 2)相关的知识,希望对你有一定的参考价值。

000000
101011
011101

D. Binary Literature
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A bitstring is a string that contains only the characters 0 and 1.

Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length 2n. A valid novel for the contest is a bitstring of length at most 3n that contains at least two of the three given strings as subsequences.

Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.

A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters.

Input
The first line contains a single integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤105).

Each of the following three lines contains a bitstring of length 2n. It is guaranteed that these three strings are pairwise distinct.

It is guaranteed that the sum of n across all test cases does not exceed 105.

Output
For each test case, print a single line containing a bitstring of length at most 3n that has at least two of the given bitstrings as subsequences.

It can be proven that under the constraints of the problem, such a bitstring always exists.

If there are multiple possible answers, you may output any of them.

Example
inputCopy
2
1
00
11
01
3
011001
111010
010001
outputCopy
010
011001010
Note
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.

In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.

错解:
//——组合方式空间利用不全面,容易忽略一些 ,无解
//000000
//101011
//011101

//#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<algorithm>
//#include<string>
//#include<sstream>
//#include<vector>
#include<map>
//#include<set>
//#include<ctype.h>
//#include<stack>
//#include<queue>
#ifdef LOCAL
FILE*FP=freopen("text.in","r",stdin);
//FILE*fp=freopen("text.out","w",stdout);
#endif
using namespace std;
//#define ll long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define pll pair<ll,ll>
#define plll pair<ll,pll> 
#define pdd pair<double,double>
#define pdi pair<double,int>
#define pid pair<int,double>
#define vi vector <int> 
#define vii vector <vi> 
#define vl vector<ll>
#define st first
#define nd second
//#define pb push_back
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a))
#define _mem(a,b,c) memset(a,b,sizeof(a[0])*c)
#define _forplus(i,a,b) for( register int i=(a); i<=(b); i++)
#define forplus(i,a,b) for( register int i=(a); i<(b); i++)
#define _forsub(i,a,b) for( register int i=(a); i>=(b); i--)
#define _forauto(a,b) for(auto &(a):(b))
#define _forautome(a,b,c) for(auto (a) = (b); (a) != (c); (a)++)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
//参考:set<int>::iterator iter = vis.begin();
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define pi (acos(-1))
#define EPS 0.00000001
#define MOD 1000000007
#define fastio 	std::ios::sync_with_stdio(false);std::cin.tie(0);
//#define int ll
#define N 300005
char a[3][N],b[3][N];
int n;
//想法:a记录,b组合,如此可以模拟找 
bool cat(char a1[],char a2[],char b[]){
	//把a1,a2组合进b,若长度大于了3*n则false,否则true;
	//——组合方式空间利用不全面,容易忽略一些 ,无解 
	//000000
	//101011
	//011101
	int pb=0,pa1=0,pa2=0;
	forplus(pa1,0,2*n){
		b[pb++]=a1[pa1];
		if(pb>3*n)return false;
		if(a1[pa1]!=a2[pa2]){
			b[pb++]=a2[pa2];
			if(pb>3*n)return false;
		}
		pa2++;	
	}
	return true;
}
int32_t main(){
	fastio
	int t;
	cin>>t;
	_forplus(i,1,t){
		//cout<<"——"<<i<<'\\n';
		cin>>n;
		_mem(b[0],0,3*n+1);
		_mem(b[1],0,3*n+1);
		_mem(b[2],0,3*n+1);
		cin>>a[0]>>a[1]>>a[2];
		/*if(t==285&&i==30)cout<<a[0]<<'\\n'<<a[1]<<'\\n'<<a[2]<<'\\n';
		if(t==285)continue;*/
		if(cat(a[0],a[1],b[0])){
			cout<<b[0]<<endl;continue; 
		}
		if(cat(a[0],a[2],b[1])){
			cout<<b[1]<<endl;continue;
		}
		if(cat(a[1],a[2],b[2])){
			cout<<b[2]<<endl;continue;
		}
		
	} 
	return 0;
}

正确:

//#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<algorithm>
//#include<string>
//#include<sstream>
//#include<vector>
#include<map>
//#include<set>
//#include<ctype.h>
//#include<stack>
//#include<queue>
#ifdef LOCAL
FILE*FP=freopen("text.in","r",stdin);
//FILE*fp=freopen("text.out","w",stdout);
#endif
using namespace std;
//#define ll long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define pll pair<ll,ll>
#define plll pair<ll,pll> 
#define pdd pair<double,double>
#define pdi pair<double,int>
#define pid pair<int,double>
#define vi vector <int> 
#define vii vector <vi> 
#define vl vector<ll>
#define st first
#define nd second
//#define pb push_back
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a))
#define _mem(a,b,c) memset(a,b,sizeof(a[0])*c)
#define _forplus(i,a,b) for( register int i=(a); i<=(b); i++)
#define forplus(i,a,b) for( register int i=(a); i<(b); i++)
#define _forsub(i,a,b) for( register int i=(a); i>=(b); i--)
#define _forauto(a,b) for(auto &(a):(b))
#define _forautome(a,b,c) for(auto (a) = (b); (a) != (c); (a)++)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
//参考:set<int>::iterator iter = vis.begin();
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define pi (acos(-1))
#define EPS 0.00000001
#define MOD 1000000007
#define fastio 	std::ios::sync_with_stdio(false);std::cin.tie(0);
//#define int ll
#define N 300005
string a[3];
int p[3];
int32_t main(){
	fastio
	int t;
	cin>>t;
	_forplus(i,1,t){
		//cout<<"——"<<i<<'\\n';
		int n;
		cin>>n;
		cin>>a[0]>>a[1]>>a[2];
		mem(p,0);
		int m=0;
		while(p[0]<2*n&&p[1]<2*n&&p[2]<2*n){
			int b=0;
			forplus(i,0,3)b+=a[i][p[i]]=='0';
			char y=(b>=2?'0':'1');
			cout<<y;
			forplus(i,0,3)p[i]+=(a[i][p[i]]==y);
			m++;
		}
		int s=m-n;//int d=3*n-m;int s=2*n-d;
		forplus(i,0,3)if(p[i]>=s&&p[i]!=2*n){
			cout<<a[i].substr(p[i]);break;
		}
		cout<<'\\n';
	} 
	return 0;
}

如何改进:
思考,2到3,可以贪心的每次有2个符合,就最坏也可以有一个按时到且有一个过半了
附:C++中substr()函数用法详解

以上是关于D. Binary Literature_Codeforces Round #715 (Div. 2)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 551 D. GukiZ and Binary Operations

D. Equal Binary Subsequences(构造)

D. Binary Spiders(dp&Trie)

D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]

CQUOJ D. 会做题的兔兔