A2. Prefix Flip (Hard Version)

A2. Prefix Flip (Hard Version)

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(ll  i=a;i<=b;i++)
#define dec(i,a,b)      for(ll  i=a;i>=b;i--)
#define forn(i, n)      for(ll i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const int mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
/**********************************************************/
const int N = 1e7 + 5;


int main() {
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        int len;
        string s1, s2;
        cin >> len >> s1 >> s2;
        if (len == 1)
        {
            if (s1 == s2)
                cout << 0 << endl;
            else
                cout << "1 1" << endl;
            continue;
        }
        int i = 0, j = s1.size() - 1;
        for (; j >= 0 && s1[j] == '0'; j--);
        int cur = 1;
        vector<int> res;
        while (i <= j)
        {
            if (cur)
            {
                for (; i <= j && s1[i] == '0'; i++);
                if (i > 0)
                    res.push_back(i);
                cur = 0;
            }
            else
            {
                for (; i <= j && s1[i] == '1'; i++);
                if (i > 0)
                    res.push_back(i);
                cur = 1;
            }
        }
        cur = 1;
        i = len-1;
        while(i>0)
        {
            if (cur)
            {
                for (; i >= 0 && s2[i] == '0'; i--);
                if (i < len && i>=0)
                    res.push_back(i+1);
                cur = 0;
            }
            else
            {
                for (; i >=0  && s2[i] == '1'; i--);
                if (i < len-1 && i>=0)
                    res.push_back(i+1);
                cur = 1;
            }
        }
        cout << res.size() <<" ";
        forn(i, res.size())
            cout << res[i] << " ";
        cout << endl;
    }
}

 

posted @ 2020-07-27 23:52  DeaL57  阅读(190)  评论(0编辑  收藏  举报