CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) C. Complementary XOR 题解

原题链接:

Problem - C - Codeforces (Unofficial mirror by Menci)

题目描述:

You have two binary strings aa and bb of length nn. You would like to make all the elements of both strings equal to 00. Unfortunately, you can modify the contents of these strings using only the following operation:

  • You choose two indices ll and rr (1≤l≤r≤n1≤l≤r≤n);
  • For every ii that respects l≤i≤rl≤i≤r, change aiai to the opposite. That is, ai:=1−aiai:=1−ai;
  • For every ii that respects either 1≤i<l1≤i<l or r<i≤nr<i≤n, change bibi to the opposite. That is, bi:=1−bibi:=1−bi.

Your task is to determine if this is possible, and if it is, to find such an appropriate chain of operations. The number of operations should not exceed n+5n+5. It can be proven that if such chain of operations exists, one exists with at most n+5n+5 operations.

Input

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051≤t≤105) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of the strings.

The second line of each test case contains a binary string aa, consisting only of characters 0 and 1, of length nn.

The third line of each test case contains a binary string bb, consisting only of characters 0 and 1, of length nn.

It is guaranteed that sum of nn over all test cases doesn't exceed 2⋅1052⋅105.

Output

For each testcase, print first "YES" if it's possible to make all the elements of both strings equal to 00. Otherwise, print "NO". If the answer is "YES", on the next line print a single integer kk (0≤k≤n+50≤k≤n+5) — the number of operations. Then kk lines follows, each contains two integers ll and rr (1≤l≤r≤n1≤l≤r≤n) — the description of the operation.

If there are several correct answers, print any of them.

Example

题目大意:

给定两个二进制字符串a和b,给定一种可执行的操作:

选定一个区间[l,r]对a的[l,r]内的字符进行取反,并对b的[1,l - 1]和[r + 1, n]进行取反。

问有没有可能在若干次操作之后使得两个字符串都变成全0.

解题思路:

我们反过来思考,初始状态a,b都是全零,通过操作能到达哪些状态.

我们随便模拟几次可以发现操作奇数次后a,b一定每一位都不同,操作偶数次后a,b一定每一位都相同.

所以只有a,b每一位都不同或者都相同才能通过操作到达全0状态.

然后我们就很容易构造出一种方案了,我的方法是先把a串全部变为0,此时如果b只可能是全0或者全1,如果是全0那就不用操作了,否则就依次操作[1,n],[1,1],[2,n],即先把a全变为1,然后按照样例的方法把全1的a,b变为全0.

代码(CPP):

#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
using LL = long long;
const int maxn = 2e5 + 5;
int a[maxn], b[maxn];

int main()
{

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            char c;
            cin >> c;
            a[i] = c - '0';
        }
        int cnt = 0;
        for (int i = 1; i <= n; i++)
        {
            char c;
            cin >> c;
            b[i] = c - '0';
            cnt += (b[i] == a[i]);
        }
        if (cnt != n && cnt != 0)
        {
            cout << "NO" << '\n';
            continue;
        }
        bool flag = cnt == n ? 0 : 1;
        vector<pair<int, int>> ans;
        for (int i = 1; i <= n; i++)
        {
            if (a[i] == 0)
                continue;
            int j = i;
            while (j + 1 <= n && a[j + 1] == 1)
                j++;
            ans.push_back({i, j});
            flag ^= 1;
            i = j;
        }
        if (flag)
        {
            ans.push_back({1, n});
            ans.push_back({1, 1});
            ans.push_back({2, n});
        }
        cout << "YES" << '\n';
        cout << ans.size() << '\n';
        for (auto [x, y] : ans)
            cout << x << ' ' << y << '\n';
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值