题目
传送门
This is the easy 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 3n 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≤1000) — 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 1000.
Output
For each test case, output an integer k (0≤k≤3n), 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.
输入
5
2
01
10
5
01011
11100
2
01
01
10
0110011011
1000110100
1
0
1
输出
3 1 2 1
6 5 2 5 3 1 2
0
9 4 1 2 10 4 1 2 1 5
1 1
题意:t组样例,每组一个n,代表字符串的长度,下面是两个字符串a,b,你可以进行操作:把字符串的前k位求反(1变成零,0变成1)然后反转前k个字符,输出操作次数和操作的每一步
思路:我们先看a,把a[i]和a[i+1]比较,如果不相同就对前i位进行操作直到整个串变成零,然后对b进行操作直到b变成零,正序输出第一个操作序列反序输出第二个操作序列就可以了,具体看代码
AC code
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<sstream>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
void solve()
{
int n;
cin>>n;
int a[1200],b[1200],l=0,r=0;
char aa[1200],bb[1200];
cin>>aa>>bb;
if(strcmp(aa,bb)==0)printf("0\n");//两个串相同
else
{
for(int i=1;i<n;i++)
if(aa[i]!=aa[i-1])
{a[l++]=i;
aa[i-1]=aa[i];}//对第一个串进行操作
if(aa[n-1]=='1')a[l++]=n;//判断最后一位是否为一,是的话还要进行一步操作
for(int i=1;i<n;i++)
if(bb[i]!=bb[i-1])
{b[r++]=i;
bb[i-1]=bb[i];}
if(bb[n-1]=='1')b[r++]=n;//操作b
printf("%d ",l+r);//总的次数
for(int i=0;i<l;i++)
{printf("%d",a[i]);//正序输出a
if(i!=l-1)printf(" ");}
if(r>=1&&l>=1)printf(" ");
for(int i=r-1;i>=0;i--)//反序输出b
{printf("%d",b[i]);
if(i!=0)printf(" ");}
printf("\n");
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
}