#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值