Getting WA with the below approach, any ideas why?

test {
        int n;
        cin >> n;
        string s;
        cin >> s;
        vi a;

        for (int i = 0; i < n; i += 2) {
            if (s[i + 1] != s[i]) {
                if (a.size())  {
                    if (s[a.back()] != s[i]) a.pb(i);
                    else a.pb(i + 1);
                } else a.pb(i);
            }
        }

        cout << a.size() << endl;
        for (auto &i : a) cout << i << " "; cout << endl;
    }

Your code is literally ok in its logic.

You just had to print it in 1-based index instead in 0-based. Literally just that.