CF1504B.Flip the Bits

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There is a binary string aa of length nn. In one operation, you can select any prefix of aa with an equal number of 00 and 11 symbols. Then all symbols in the prefix are inverted: each 00 becomes 11 and each 11 becomes 00.

For example, suppose a=0111010000a=0111010000.

  • In the first operation, we can select the prefix of length 88 since it has four 00's and four 11's: [01110100]00[10001011]00[01110100]00\to [10001011]00.
  • In the second operation, we can select the prefix of length 22 since it has one 00 and one 11: [10]00101100[01]00101100[10]00101100\to [01]00101100.
  • It is illegal to select the prefix of length 44 for the third operation, because it has three 00's and one 11.

Can you transform the string aa into the string bb using some finite number of operations (possibly, none)?

给定一个长度为 nn 的二进制字符串 aa。在一次操作中,你可以选择 aa 的任意一个前缀,该前缀中 0011 的个数必须相等;然后将该前缀中的所有符号翻转:每个 00 变为 11,每个 11 变为 00

例如,设 a=0111010000a=0111010000

  • 在第一次操作中,我们可以选择长度为 88 的前缀,因为它包含四个 00 和四个 11[01110100]00[10001011]00[01110100]00\to [10001011]00
  • 在第二次操作中,我们可以选择长度为 22 的前缀,因为它包含一个 00 和一个 11[10]00101100[01]00101100[10]00101100\to [01]00101100
  • 第三次操作中选择长度为 44 的前缀是非法的,因为该前缀包含三个 00 和一个 11

你能否通过有限次(可能为零次)上述操作,将字符串 aa 变换为字符串 bb

输入格式

The first line contains a single integer tt (1t1041\le t\le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1n31051\le n\le 3\cdot 10^5) — the length of the strings aa and bb.

The following two lines contain strings aa and bb of length nn, consisting of symbols 00 and 11.

The sum of nn across all test cases does not exceed 31053\cdot 10^5.

第一行包含一个整数 tt1t1041\le t\le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn1n31051\le n\le 3\cdot 10^5)—— 字符串 aabb 的长度。

接下来的两行分别包含长度为 nn 的字符串 aabb,均由字符 0011 组成。

所有测试用例的 nn 之和不超过 31053\cdot 10^5

输出格式

For each test case, output "YES" if it is possible to transform aa into bb, or "NO" if it is impossible. You can print each letter in any case (upper or lower).

对于每个测试用例,如果可以将 aa 变换为 bb,则输出 "YES";否则输出 "NO"。你可以以任意大小写(大写或小写)输出每个字母。

输入输出样例

  • 输入#1

    5
    10
    0111010000
    0100101100
    4
    0000
    0000
    3
    001
    000
    12
    010101010101
    100110011010
    6
    000111
    110100

    输出#1

    YES
    YES
    NO
    YES
    NO

说明/提示

The first test case is shown in the statement.

In the second test case, we transform aa into bb by using zero operations.

In the third test case, there is no legal operation, so it is impossible to transform aa into bb.

In the fourth test case, here is one such transformation:

  • Select the length 22 prefix to get 100101010101100101010101.
  • Select the length 1212 prefix to get 011010101010011010101010.
  • Select the length 88 prefix to get 100101011010100101011010.
  • Select the length 44 prefix to get 011001011010011001011010.
  • Select the length 66 prefix to get 100110011010100110011010.

In the fifth test case, the only legal operation is to transform aa into 111000111000. From there, the only legal operation is to return to the string we started with, so we cannot transform aa into bb.

第一个测试用例已在题目描述中给出。

在第二个测试用例中,我们无需执行任何操作即可将 aa 变换为 bb

在第三个测试用例中,不存在合法的操作,因此无法将 aa 变换为 bb

在第四个测试用例中,存在如下一种变换方式:

  • 选择长度为 22 的前缀,得到 100101010101100101010101
  • 选择长度为 1212 的前缀,得到 011010101010011010101010
  • 选择长度为 88 的前缀,得到 100101011010100101011010
  • 选择长度为 44 的前缀,得到 011001011010011001011010
  • 选择长度为 66 的前缀,得到 100110011010100110011010

在第五个测试用例中,唯一合法的操作是将 aa 变换为 111000111000;此后,唯一合法的操作是变回初始字符串,因此无法将 aa 变换为 bb

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

首页