Codeforces Round #658 (Div. 1) A2. Prefix Flip (Hard Version)

题目链接

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≤1e5) — 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

这题发现题解的思路真的妙,将字符串 a a a 转为 0 0 0 需要步骤 s 1 s1 s1,将字符串 b b b 转为 0 0 0 需要步骤 s 2 s2 s2,那么只要将 s 1 s1 s1 加上倒置的 s 2 s2 s2 即可,AC代码如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{int t,n;string a,b;cin>>t;while(t--){cin>>n>>a>>b;a+='0',b+='0';vector<int>v1,v2;for(int i=1;i<=n;i++){if(a[i]!=a[i-1]) v1.push_back(i);if(b[i]!=b[i-1]) v2.push_back(i);}v1.insert(v1.end(),v2.rbegin(),v2.rend());cout<<v1.size()<<endl;for(auto i:v1) cout<<" "<<i;puts("");}
}

Codeforces Round #658 (Div. 1) A2. Prefix Flip (Hard Version)相关推荐

  1. #658 (Div. 2) C2.Prefix Flip (Hard Version)

    题目描述 This is the hard version of the problem. The difference between the versions is the constraint ...

  2. Codeforces Round #658 (Div. 2) D. Unmerge 思维 + 背包

    传送门 文章目录 题意: 思路: 题意: 思路: 我们可以假设有两个桶,让后向里面放数,可以发现,如果向其中放入了aia_iai​,那么紧跟在他后面的所有<ai<a_i<ai​的数都 ...

  3. Codeforces Round #658 (Div. 2)部分题解

    文章目录 A - Common Subsequence B - Sequential Nim C1 - Prefix Flip (Easy Version) C2 - Prefix Flip (Har ...

  4. Codeforces Round #658 (Div. 2)

    A - Common Subsequence 最短相同子序列长度肯定为1,如果一个元素都不相等之间不存在相同子序列 #define IO ios::sync_with_stdio(false);cin ...

  5. Codeforces Round #620 (Div. 2) F2. Animal Observation (hard version) dp + 线段树

    传送门 文章目录 题意: 思路: 题意: 比如下面这个图: 思路: 对于这个题,比较容易就能考虑到dpdpdp,设f[i][j]f[i][j]f[i][j]为到了第iii行,覆盖了[j,j+k−1][ ...

  6. Codeforces Round #617 (Div. 3) E2. String Coloring (hard version) 思维 + dp + Dilworth定理

    传送门 文章目录 题意: 思路: 题意: 让你给一个串染色,不同颜色且相邻的一对字符可以互换位置,用最少的颜色,使交换后这个字符串字典序最小. 思路: 考虑将字符串分成若干个非递减的子序列,由于其非递 ...

  7. Codeforces Round #658 (Div. 2) D. Unmerge(分块,背包dp)

    题目传送 思路: 首先得观察到一个规律,如果某个数的后面紧跟着的数中有一段比他小的数,那么这一段数肯定是在一个数组中的,例: 5 3 1 4 2 10 7 8 9 6 ,那么可以分成三段 [5 3 1 ...

  8. 差分 ---- Codeforces Round #672 (Div. 2):C2. Pokémon Army (hard version)[差分的思想]

    题目链接 题目大意:就算给你一序列,按照顺序出若干个数组成一个的序列,然后对这个序列定义一个权值就算奇数位置的和减去偶数位置的和,问你能的到的最大的权值是多少? **a1 - a2 + a3 - a4 ...

  9. Codeforces Round # 555 (Div. 3) C2. Increasing subsequence (complicated version) (贪心)

    题目链接:http://codeforces.com/contest/1157/problem/C2 当左右两边数字相同时,需要判断一下取哪边能得到更长的递增序列 #include <iostr ...

最新文章

  1. atom和phpcs
  2. linux c 报错 multiple definition of ‘xxx’ 解决方法
  3. oracle走当前时间分区,Oracle分区使用波斯日历的时间间隔
  4. php 其他页面获取session_PHP五十个提升执行效率的小技巧,和常见问题
  5. 定期存款可以提前取出来吗_如果银行存款利率涨了,定期存款有必要取出再存吗?...
  6. 指针用完了怎么办_墙体里水管漏水怎么办?瞧瞧这个,轻松搞定,滴水不漏
  7. jvm 虚拟机的组成部分
  8. excel数据导入到 mysql 中
  9. Raki的读paper小记:PURE:A Frustratingly Easy Approach for Entity and Relation Extraction
  10. 通过8uftp客户端连接FTP服务器来感受防火墙下FTP主动模式和被动模式的区别。
  11. e7 88系列服务器,英特尔至强E7处理器性能多项测试比拼
  12. 连接到linux桌面,widows 远程连接到linux 桌面!
  13. Zabbix-2.4-安装-1
  14. unity中碰撞检测方法
  15. (无Xcode)Mac OS X中解决 make,gcc等命令找不到
  16. 从零开始的微信小程序入门教程
  17. 视频会议检测不到摄像头,电脑打开相关功能提示无法使用-驱动人生解决方案
  18. (批处理)使用bat如何取出文件夹里子文件夹下的文件?
  19. NIO空轮训出现的原理以及修复方案
  20. 对面向对象程序设计(OOP)的认识

热门文章

  1. 蜗牛圈圈-时尚智能的运动计时App
  2. 图像色彩增强论文调研
  3. 赤峰php,赤峰php程序员培训,赤峰php程序员培训中心,赤峰php程序员培训哪家比较好...
  4. 股票婚后增值,可否请求分割增值部分
  5. 库客音乐冲刺美股:业绩下滑明显,曾两度赴港,余赫持股约30%
  6. python编程从入门到实践,第七章练习题
  7. python编程:从入门到实践 7-8、7-10
  8. 开关电源为什么要老化测试
  9. oracle常见面试题
  10. Unity接入科大讯飞SDK-安卓篇