CF1750C.Complementary XOR

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 \le l \le r \le n);
  • For every ii that respects l≤i≤rl \le i \le r, change aia_i to the opposite. That is, ai:=1−aia_i := 1 - a_i;
  • For every ii that respects either 1≤i<l1 \le i \lt l or r<i≤nr \lt i \le n, change bib_i to the opposite. That is, bi:=1−bib_i := 1 - b_i.

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.

你有两个长度为 nn 的二进制字符串 aa 和 bb。你的目标是使这两个字符串的所有元素都变为 00。不幸的是,你只能使用以下操作来修改这两个字符串的内容:

  • 选择两个下标 ll 和 rr(满足 1≤l≤r≤n1 \le l \le r \le n);
  • 对每个满足 l≤i≤rl \le i \le r 的 ii,将 aia_i 翻转(即 ai:=1−aia_i := 1 - a_i);
  • 对每个满足 1≤i<l1 \le i < l 或 r<i≤nr < i \le n 的 ii,将 bib_i 翻转(即 bi:=1−bib_i := 1 - b_i)。

你的任务是判断该目标是否可达;若可达,则需找出一串满足要求的操作序列。操作次数不得超过 n+5n + 5。可以证明:若存在可行的操作序列,则必存在一个操作次数不超过 n+5n + 5 的可行序列。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051 \leq t \leq 10^5) — 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 \le n \le 2 \cdot 10^5) — 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 \cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5),表示字符串的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 aa,仅由字符 0 和 1 组成。

每个测试用例的第三行包含一个长度为 nn 的二进制字符串 bb,仅由字符 0 和 1 组成。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

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 \le k \le n + 5) — the number of operations. Then kk lines follows, each contains two integers ll and rr (1≤l≤r≤n1 \le l \le r \le n) — the description of the operation.

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

对于每个测试用例,如果可以将两个字符串的所有元素都变为 00,则首先输出 "YES";否则输出 "NO"。如果答案为 "YES",则在下一行输出一个整数 kk(0≤k≤n+50 \le k \le n + 5),表示操作次数;随后输出 kk 行,每行包含两个整数 ll 和 rr(1≤l≤r≤n1 \le l \le r \le n),表示一次操作的区间。

若存在多个正确答案,输出任意一个即可。

输入输出样例

  • 输入#1

    5
    3
    010
    101
    2
    11
    10
    4
    1000
    0011
    2
    10
    10
    3
    111
    111

    输出#1

    YES
    1
    2 2
    NO
    NO
    YES
    2
    1 2
    2 2
    YES
    2
    1 1
    2 3

说明/提示

In the first test case, we can perform one operation with l=2l = 2 and r=2r = 2. So a2:=1−1=0a_2 := 1 - 1 = 0 and string aa became equal to 000. b1:=1−1=0b_1 := 1 - 1 = 0, b3:=1−1=0b_3 := 1 - 1 = 0 and string bb became equal to 000.

In the second and in the third test cases, it can be proven that it's impossible to make all elements of both strings equal to 00.

In the fourth test case, we can perform an operation with l=1l = 1 and r=2r = 2, then string aa became equal to 01, and string bb doesn't change. Then we perform an operation with l=2l = 2 and r=2r = 2, then a2:=1−1=0a_2 := 1 - 1 = 0 and b1=1−1=0b_1 = 1 - 1 = 0. So both of string aa and bb became equal to 00.

In the fifth test case, we can perform an operation with l=1l = 1 and r=1r = 1. Then string aa became equal to 011 and string bb became equal to 100. Then we can perform an operation with l=2l = 2 and r=3r = 3, so both of string aa and bb became equal to 000.

在第一个测试用例中,我们可以执行一次操作,其中 l=2l = 2 且 r=2r = 2。于是 a2:=1−1=0a_2 := 1 - 1 = 0,字符串 aa 变为 000;b1:=1−1=0b_1 := 1 - 1 = 0,b3:=1−1=0b_3 := 1 - 1 = 0,字符串 bb 也变为 000。

在第二个和第三个测试用例中,可以证明无法使两个字符串的所有元素均变为 00。

在第四个测试用例中,我们首先执行一次操作,其中 l=1l = 1 且 r=2r = 2,此时字符串 aa 变为 01,而字符串 bb 保持不变;接着再执行一次操作,其中 l=2l = 2 且 r=2r = 2,于是 a2:=1−1=0a_2 := 1 - 1 = 0 且 b1:=1−1=0b_1 := 1 - 1 = 0。最终,字符串 aa 和 bb 均变为 00。

在第五个测试用例中,我们首先执行一次操作,其中 l=1l = 1 且 r=1r = 1,此时字符串 aa 变为 011,字符串 bb 变为 100;然后执行一次操作,其中 l=2l = 2 且 r=3r = 3,于是字符串 aa 和 bb 均变为 000。

输入解题思路,AI测评打分。不知道怎么写?

首页