C1
暴力可过
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int maxn = 200010;
int a[maxn];
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
string s1,s2;
cin >> s1 >> s2;
if(s1 == s2){
cout << "0" << endl;
continue;
}
int k = 0;
for(int i = n-1;i >= 0;i--){
if(s2[i] == s1[i]) continue;
if(s2[i] == s1[0]){
a[k++] = 1;
if(s1[0] == '0') s1[0] = '1';
else s1[0] = '0';
}
for(int j = 0;j <= i;j++){
if(s1[j] == '0') s1[j] = '1';
else s1[j] = '0';
}
for(int j = 0;j <= i/2;j++){
char tt = s1[j];
s1[j] = s1[i-j];
s1[i-j] = tt;
}
a[k++] = i+1;
}
cout << k;
for(int i = 0;i<k;i++){
cout << " " << a[i];
}
cout << endl;
}
return 0;
}
C2
树状数组优化
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = 200010;
int a[maxn];
int c[maxn/2];
int n;
int ask(int x){
int ans = 0;
for(;x;x -= x & -x) ans += c[x];
return ans;
}
void add(int x,int y){
for(;x <= n;x += x & -x){
c[x] += y;
}
}
int main(){
int t;
cin >> t;
while(t--){
cin >> n;
vector<int> b(n+10,0);
memset(c,0,sizeof(int)*(n+10));
string s1,s2;
cin >> s1 >> s2;
if(s1 == s2){
cout << "0" << endl;
continue;
}
int k = 0;
int flag = 0;//正
int l = 0;
int r = n-1;
for(int i = n-1;i >= 0;i--){
if(flag == 0) {
char tt = s1[r];
int ans = ask(r+1);
if(ans%2 == 1) {
if(tt == '1') tt = '0';
else tt = '1';
}
if(s2[i] == tt){
r--;
continue;
}
}
if(flag == 1) {
char tt = s1[l];
int ans = ask(l+1);
if(ans%2 == 1) {
if(tt == '1') tt = '0';
else tt = '1';
}
if(s2[i] == tt){
l++;
continue;
}
}
if(flag == 0){
char tt = s1[l];
int ans = ask(l+1);
if(ans%2 == 1) {
if(tt == '1') tt = '0';
else tt = '1';
}
if(s2[i] == s1[l]){
a[k++] = 1;
}
add(l+1,1);
add(l+i+2,-1);
}
else{
char tt = s1[r];
int ans = ask(r+1);
if(ans%2 == 1) {
if(tt == '1') tt = '0';
else tt = '1';
}
if(s2[i] == tt){
a[k++] = 1;
}
add(r+2,-1);
add(r-i+1,1);
}
a[k++] = i+1;
flag ^= 1;//翻转
if(flag == 0) r--;
else l++;
}
cout << k;
for(int i = 0;i<k;i++){
cout << " " << a[i];
}
cout << endl;
}
return 0;
}