CF1750C.Complementary XOR
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have two binary strings a and b of length n. You would like to make all the elements of both strings equal to 0. Unfortunately, you can modify the contents of these strings using only the following operation:
- You choose two indices l and r (1≤l≤r≤n);
- For every i that respects l≤i≤r, change ai to the opposite. That is, ai:=1−ai;
- For every i that respects either 1≤i<l or r<i≤n, change bi to the opposite. That is, bi:=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+5. It can be proven that if such chain of operations exists, one exists with at most n+5 operations.
你有两个长度为 n 的二进制字符串 a 和 b。你的目标是使这两个字符串的所有元素都变为 0。不幸的是,你只能使用以下操作来修改这两个字符串的内容:
- 选择两个下标 l 和 r(满足 1≤l≤r≤n);
- 对每个满足 l≤i≤r 的 i,将 ai 翻转(即 ai:=1−ai);
- 对每个满足 1≤i<l 或 r<i≤n 的 i,将 bi 翻转(即 bi:=1−bi)。
你的任务是判断该目标是否可达;若可达,则需找出一串满足要求的操作序列。操作次数不得超过 n+5。可以证明:若存在可行的操作序列,则必存在一个操作次数不超过 n+5 的可行序列。
输入格式
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤105) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the length of the strings.
The second line of each test case contains a binary string a, consisting only of characters 0 and 1, of length n.
The third line of each test case contains a binary string b, consisting only of characters 0 and 1, of length n.
It is guaranteed that sum of n over all test cases doesn't exceed 2⋅105.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤105),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105),表示字符串的长度。
每个测试用例的第二行包含一个长度为 n 的二进制字符串 a,仅由字符 0 和 1 组成。
每个测试用例的第三行包含一个长度为 n 的二进制字符串 b,仅由字符 0 和 1 组成。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each testcase, print first "YES" if it's possible to make all the elements of both strings equal to 0. Otherwise, print "NO". If the answer is "YES", on the next line print a single integer k (0≤k≤n+5) — the number of operations. Then k lines follows, each contains two integers l and r (1≤l≤r≤n) — the description of the operation.
If there are several correct answers, print any of them.
对于每个测试用例,如果可以将两个字符串的所有元素都变为 0,则首先输出 "YES";否则输出 "NO"。如果答案为 "YES",则在下一行输出一个整数 k(0≤k≤n+5),表示操作次数;随后输出 k 行,每行包含两个整数 l 和 r(1≤l≤r≤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=2 and r=2. So a2:=1−1=0 and string a became equal to 000. b1:=1−1=0, b3:=1−1=0 and string b 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 0.
In the fourth test case, we can perform an operation with l=1 and r=2, then string a became equal to 01, and string b doesn't change. Then we perform an operation with l=2 and r=2, then a2:=1−1=0 and b1=1−1=0. So both of string a and b became equal to 00.
In the fifth test case, we can perform an operation with l=1 and r=1. Then string a became equal to 011 and string b became equal to 100. Then we can perform an operation with l=2 and r=3, so both of string a and b became equal to 000.
在第一个测试用例中,我们可以执行一次操作,其中 l=2 且 r=2。于是 a2:=1−1=0,字符串 a 变为 000;b1:=1−1=0,b3:=1−1=0,字符串 b 也变为 000。
在第二个和第三个测试用例中,可以证明无法使两个字符串的所有元素均变为 0。
在第四个测试用例中,我们首先执行一次操作,其中 l=1 且 r=2,此时字符串 a 变为 01,而字符串 b 保持不变;接着再执行一次操作,其中 l=2 且 r=2,于是 a2:=1−1=0 且 b1:=1−1=0。最终,字符串 a 和 b 均变为 00。
在第五个测试用例中,我们首先执行一次操作,其中 l=1 且 r=1,此时字符串 a 变为 011,字符串 b 变为 100;然后执行一次操作,其中 l=2 且 r=3,于是字符串 a 和 b 均变为 000。
输入解题思路,AI测评打分。不知道怎么写?