当前位置: 首页 > news >正文

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

	

相关文章:

  • 【大话设计模式】工厂方法模式
  • (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
  • DSPE-PEG-iRGD,iRGD-PEG-DSPE,磷脂-聚乙二醇-靶向肽iRGD,一种磷脂PEG肽
  • 自助商业智能平台 HK-Visokio Omniscope 详解!
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • java优先级队列PriorityQueue
  • 537、RabbitMQ详细入门教程系列 -【消费者Consumer(一)】 2022.08.31
  • wxpython分页
  • Java输入/输出之RandomAccessFile的功能和用法
  • RNNGNULSTM与PyTorch
  • Python升级之路( Lv15 ) 并发编程三剑客: 进程, 线程与协程
  • 南大通用GBase 8a MPP Cluster开发接口简介
  • IntelliJ IDEA 插件推荐
  • Rt-Thread 启动流程与组件初始化
  • CentOS-7-x86_64 iso镜像的安装(Linux操作系统)
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • HTTP 简介
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • Java 多线程编程之:notify 和 wait 用法
  • JS字符串转数字方法总结
  • KMP算法及优化
  • nodejs实现webservice问题总结
  • PV统计优化设计
  • Python学习之路13-记分
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • 前端知识点整理(待续)
  • 实习面试笔记
  • 微信小程序开发问题汇总
  • 小程序button引导用户授权
  • 小试R空间处理新库sf
  • Prometheus VS InfluxDB
  • 仓管云——企业云erp功能有哪些?
  • 从如何停掉 Promise 链说起
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • ${ }的特别功能
  • $forceUpdate()函数
  • (06)Hive——正则表达式
  • (1)(1.8) MSP(MultiWii 串行协议)(4.1 版)
  • (2)MFC+openGL单文档框架glFrame
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (ibm)Java 语言的 XPath API
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (五)Python 垃圾回收机制
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (转载)Linux网络编程入门
  • ./configure,make,make install的作用
  • .Net IOC框架入门之一 Unity
  • .NET MVC之AOP
  • .NET Remoting Basic(10)-创建不同宿主的客户端与服务器端
  • .Net 代码性能 - (1)
  • .net 反编译_.net反编译的相关问题
  • .net 简单实现MD5
  • .NET 将多个程序集合并成单一程序集的 4+3 种方法
  • .NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试