#658 (Div. 2) C2.Prefix Flip (Hard Version)
题目描述
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix.
For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001.
Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible.
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases. Next 3t lines contain descriptions of test cases.
The first line of each test case contains a single integer n (1≤n≤105) — the length of the binary strings.
The next two lines contain two binary strings a and b of length n.
It is guaranteed that the sum of n across all test cases does not exceed 105.
Output
For each test case, output an integer k (0≤k≤2n), followed by k integers p1,…,pk (1≤pi≤n). Here k is the number of operations you use and pi is the length of the prefix you flip in the i-th operation.
Example
Input
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
Output
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
Note
In the first test case, we have 01→11→00→10.
In the second test case, we have 01011→00101→11101→01000→10100→00100→11100.
In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
题目大意
给你一个字符串a和b,每次你都可以选择前缀n,然后将前n个位翻转(从’0’变为‘1’),然后再整体反转,比如0111 =>1110,让你在2*n步里面从a变到b然后让你输出方案。
题目分析
我们可以把a中字符全部变成0或1,然后记录反转的过程。再把b中字符全部变成0或1,再记录反转的过程。
从b变成全0或全1的过程是可逆的,因此把a变成全0或全1,再把把b变成全0或全1过程逆转过来,即得到了把a变成b的过程。
这个做法真的很简单,就是能不能想到的问题了。我在做的时候确实是没有想到这个方法。(我做的时候被我自己想的O(n2)的方法给带跑了。。。)
代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
#define PII pair<int,int>
using namespace std;
const int N=2e5+5;
int k1,k2;
int f[N],g[N]; //f[]记录把a变成全0或者全1的过程,g[]记录b的
int main()
{cin.tie(0);ios::sync_with_stdio(false);int t;cin>>t;while(t--){k1=k2=0;int n;string a,b;cin>>n>>a>>b;for(int i=1;i<n;i++) //把a变成全0或者全1的过程if(a[i]!=a[i-1]) f[k1++]=i;for(int i=1;i<n;i++) //把b变成全0或者全1的过程if(b[i]!=b[i-1]) g[k2++]=i;//如果a与b最后变得是相反的(一个全0,另一个全1),则要把a反转过去if(a[n-1]!=b[n-1]) f[k1++]=n;cout<<k1+k2<<' ';for(int i=0;i<k1;i++) cout<<f[i]<<' '; //a变的过程正序输出for(int i=k2-1;i>=0;i--) cout<<g[i]<<' '; //b变的过程逆序输出cout<<endl;}return 0;
}
#658 (Div. 2) C2.Prefix Flip (Hard Version)相关推荐
- Codeforces Round #658 (Div. 1) A2. Prefix Flip (Hard Version)
Codeforces Round #658 (Div. 1) A2. Prefix Flip (Hard Version) 题目链接 There are two binary strings a an ...
- Codeforces C1. Prefix Flip (Easy Version) (二进制串 / 模拟 / 构造) (Roun #658 Div.2)
传送门 题意: 给出两个长度为n的二进制串a和b,你每次可选取一段前缀子串取反并翻转(即:10010 -> 01101 -> 10110).已知在3 * n次操作内一定能将a变成b.先让你 ...
- Codeforces Round # 555 (Div. 3) C2. Increasing subsequence (complicated version) (贪心)
题目链接:http://codeforces.com/contest/1157/problem/C2 当左右两边数字相同时,需要判断一下取哪边能得到更长的递增序列 #include <iostr ...
- 差分 ---- Codeforces Round #672 (Div. 2):C2. Pokémon Army (hard version)[差分的思想]
题目链接 题目大意:就算给你一序列,按照顺序出若干个数组成一个的序列,然后对这个序列定义一个权值就算奇数位置的和减去偶数位置的和,问你能的到的最大的权值是多少? **a1 - a2 + a3 - a4 ...
- Codeforces Round #672 (Div. 2) C2 - Pokémon Army (hard version)(贪心,维护变化值)
x数组里选一个子数组y(原数组顺序),y1-y2+y3-y4+- 的最大值 然后还有q次交换操作,每次修改之后都要输出新的最大值 (1)如果没有修改,单纯对于当前数组考虑,我们最后选出来的点肯定是波峰 ...
- Codeforces Round #658 (Div. 2)部分题解
文章目录 A - Common Subsequence B - Sequential Nim C1 - Prefix Flip (Easy Version) C2 - Prefix Flip (Har ...
- Codeforces Round #658 (Div. 2)
A - Common Subsequence 最短相同子序列长度肯定为1,如果一个元素都不相等之间不存在相同子序列 #define IO ios::sync_with_stdio(false);cin ...
- Codeforces Round #568 (Div. 2)C2. Exam in BerSU (hard version)
Codeforces Round #568 (Div. 2)C2. Exam in BerSU (hard version) 贪心+暴力 大致题意:N个人考试,每个人花费的时间是a[i],他们总共花费 ...
- Codeforces Round #555 (Div. 3) c2 d e f
c2:Increasing Subsequence (hard version) 那边小取那边,然后相等比较后面的长度 #include<bits/stdc++.h> using name ...
最新文章
- Linux下JDK环境的配置
- Hive中JOIN操作
- 深入浅出多线程系列之四:简单的同步 lock
- Discuz!论坛,如何查看全站最新帖子列表?
- This version of CLI is only compatible with Angular versions
- YouTube增加社交功能:邀请联系人聊天 可30人群聊
- Google BERT应用之《红楼梦》对话人物提取
- 利用Python延迟初始化提升性能
- mac系统更新后code .命令打不开vs code
- Scroll Dialog
- javascript基本函数
- TRIPLE is More Than DOUBLE Plus One
- ATK插件化开发:AtkPlug,AtkSocket
- 2021李宏毅机器学习课程笔记——Explainable AI
- 计算机组成原理学习-哈工大《计算机组成原理》第一章
- 一文读懂YUV的采样与格式
- 搜索引擎 —海量数据搜索
- 笔记本此计算机到网络出现一个叉,笔记本电脑无线网络不可用并显示红叉的解决方...
- 一只鸟就这样耍弄了一个人
- Bug复现辅助神器-EV录屏
热门文章
- QTextEdit更改特定某些行的字体格式
- staruml2.8 破解
- realme支持鸿蒙系统,小米、OPPO、vivo、魅族、Realme在搭载华为鸿蒙系统上的态度...
- C语言的编译,汇编和链接
- Genymotion无法添加虚拟手机设备解决方式
- 华创网表v6.8无捆绑官方正式版
- 小米的android版本是多少合适,2019年了国产安卓里哪款手机系统最好,小米MIUI还是华为?...
- Kotlin相关面试题
- 9款最好的基于Rails的CMS内容管理系统
- js跳出for双循环