CF1861B.Two Binary Strings

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two strings aa and bb of equal length, consisting of only characters 0 and/or 1; both strings start with character 0 and end with character 1.

You can perform the following operation any number of times (possibly zero):

  • choose one of the strings and two equal characters in it; then turn all characters between them into those characters.

Formally, you choose one of these two strings (let the chosen string be ss), then pick two integers ll and rr such that 1≤l<r≤∣s∣1 \le l \lt r \le |s| and sl=srs_l = s_r, then replace every character sis_i such that l<i<rl \lt i \lt r with sls_l.

For example, if the chosen string is 010101, you can transform it into one of the following strings by applying one operation:

  • 000101 if you choose l=1l = 1 and r=3r = 3;
  • 000001 if you choose l=1l = 1 and r=5r = 5;
  • 010001 if you choose l=3l = 3 and r=5r = 5;
  • 010111 if you choose l=4l = 4 and r=6r = 6;
  • 011111 if you choose l=2l = 2 and r=6r = 6;
  • 011101 if you choose l=2l = 2 and r=4r = 4.

You have to determine if it's possible to make the given strings equal by applying this operation any number of times.

给你两个长度相等的字符串 aa 和 bb,它们仅由字符 0 和/或 1 组成;且两个字符串均以字符 0 开头、以字符 1 结尾。

你可以执行以下操作任意多次(包括零次):

  • 选择其中一个字符串,并在其中选取两个相等的字符;然后将这两个字符之间的所有字符都变为该字符。

形式化地,你选择这两个字符串中的一个(设其为 ss),再选取两个整数 ll 和 rr,满足 1≤l<r≤∣s∣1 \le l \lt r \le |s| 且 sl=srs_l = s_r,然后将所有满足 l<i<rl \lt i \lt r 的字符 sis_i 替换为 sls_l。

例如,若所选字符串为 010101,则通过一次操作可将其变为以下字符串之一:

  • 000101(当选择 l=1l = 1 且 r=3r = 3 时);
  • 000001(当选择 l=1l = 1 且 r=5r = 5 时);
  • 010001(当选择 l=3l = 3 且 r=5r = 5 时);
  • 010111(当选择 l=4l = 4 且 r=6r = 6 时);
  • 011111(当选择 l=2l = 2 且 r=6r = 6 时);
  • 011101(当选择 l=2l = 2 且 r=4r = 4 时)。

你需要判断:是否可以通过任意次执行该操作,使得给定的两个字符串变得相等。

输入格式

The first line contains a single integer tt (1≤t≤20001 \le t \le 2000) — the number of test cases.

Each test case consists of two lines:

  • the first line contains the string aa (2≤∣a∣≤50002 \le |a| \le 5000), consisting of only characters 0 and/or 1.
  • the second line contains the string bb (2≤∣b∣≤50002 \le |b| \le 5000), consisting of only characters 0 and/or 1.

Additional constraints on the input:

  • in each test case, ∣a∣=∣b∣|a| = |b| (the strings have equal length);
  • in each test case, both strings start with 0 and end with 1;
  • the total length of all strings aa in all test cases does not exceed 50005000.

第一行包含一个整数 tt(1≤t≤20001 \le t \le 2000)—— 测试用例的数量。

每个测试用例由两行组成:

  • 第一行包含字符串 aa(2≤∣a∣≤50002 \le |a| \le 5000),仅由字符 0 和/或 1 组成;
  • 第二行包含字符串 bb(2≤∣b∣≤50002 \le |b| \le 5000),仅由字符 0 和/或 1 组成。

输入的额外约束条件:

  • 在每个测试用例中,∣a∣=∣b∣|a| = |b|(两个字符串长度相等);
  • 在每个测试用例中,两个字符串均以 0 开头、以 1 结尾;
  • 所有测试用例中字符串 aa 的总长度不超过 50005000。

输出格式

For each test case, print YES if it is possible to make the strings equal. Otherwise, print NO. You can print each letter in any register.

对于每个测试用例,如果可以使两个字符串相等,则输出 YES;否则输出 NO。你可以以任意大小写形式输出每个字母。

输入输出样例

  • 输入#1

    7
    01010001
    01110101
    01001
    01001
    000101
    010111
    00001
    01111
    011
    001
    001001
    011011
    010001
    011011

    输出#1

    YES
    YES
    YES
    NO
    NO
    NO
    YES

说明/提示

In the first test case, we can perform the following operations:

  1. choose the string aa, l=2l = 2, r=4r = 4; after this operation, aa is 01110001, bb is 01110101;
  2. choose the string bb, l=5l = 5, r=7r = 7; after this operation, aa is 01110001, bb is 01110001.

In the second test case, the strings are already equal.

In the third test case, we can perform the following operations:

  1. choose the string aa, l=4l = 4, r=6r = 6; after this operation, aa is 000111, bb is 010111;
  2. choose the string bb, l=1l = 1, r=3r = 3; after this operation, aa is 000111, bb is 000111;

In the fourth and fifth test cases, it's impossible to make the given strings equal.

在第一个测试用例中,我们可以执行以下操作:

  1. 选择字符串 aa,l=2l = 2,r=4r = 4;执行该操作后,aa 变为 01110001,bb 变为 01110101;
  2. 选择字符串 bb,l=5l = 5,r=7r = 7;执行该操作后,aa 为 01110001,bb 为 01110001。

在第二个测试用例中,两个字符串已经相等。

在第三个测试用例中,我们可以执行以下操作:

  1. 选择字符串 aa,l=4l = 4,r=6r = 6;执行该操作后,aa 变为 000111,bb 变为 010111;
  2. 选择字符串 bb,l=1l = 1,r=3r = 3;执行该操作后,aa 为 000111,bb 为 000111。

在第四和第五个测试用例中,无法使给定的字符串相等。

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

首页