CF2158D.Palindrome Flipping

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two binary strings ss and tt, each of the same length nn. You are allowed to perform the following operation:

  • Pick indices ll, rr (1≤l<r≤n1 \le \color{red}{l \lt r} \le n) such that substring sl,rs_{l,r} is a palindrome and flip all bits in substring sl,rs_{l,r}.

The goal is to finally make ss equal to tt, performing any of the above operations at most 2n2n times (possibly none).

A substring sl,rs_{l,r} of a string ss is the contiguous sequence of characters starting from index ll and ending at index rr (both inclusive), where 1≤l<r≤∣s∣1 \leq l \lt r \leq |s|. Here ∣s∣|s| denotes the length of the string ss.

A string is a palindrome if it reads the same forwards and backwards. For example, the strings 101 and 00 are palindromes, while 10 is not.

Flipping all bits in a substring means changing each 0 to 1 and each 1 to 0 in that substring. For example, flipping the substring 101 results in 010.

给你两个长度均为 nn 的二进制字符串 ss 和 tt。你可以执行以下操作:

  • 选择下标 ll、rr(满足 1≤l<r≤n1 \le \color{red}{l \lt r} \le n),使得子串 sl,rs_{l,r} 是一个回文串,并将子串 sl,rs_{l,r} 中的所有位取反。

目标是通过至多 2n2n 次(可以为零次)上述操作,使 ss 最终等于 tt。

字符串 ss 的子串 sl,rs_{l,r} 是指从下标 ll 开始、到下标 rr 结束(均包含)的连续字符序列,其中 1≤l<r≤∣s∣1 \leq l \lt r \leq |s|。这里 ∣s∣|s| 表示字符串 ss 的长度。

若一个字符串正读与反读完全相同,则称其为回文串。例如,字符串 101 和 00 是回文串,而 10 不是。

对子串中所有位取反,是指将该子串中的每个 0 变为 1,每个 1 变为 0。例如,对子串 101 取反后得到 010。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases TT (1≤T≤5⋅1031 \le T \le 5\cdot 10^3). The description of the test cases follows.

The first line of each test case contains an integer nn (4≤n≤1004 \le n \le 100) — the length of the strings ss and tt.

The next two lines of each test case contain the binary strings ss and tt, respectively.

It is guaranteed that the sum of n2n^2 over all test cases does not exceed 5⋅1055\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 TT(1≤T≤5⋅1031 \le T \le 5\cdot 10^3)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(4≤n≤1004 \le n \le 100)—— 字符串 ss 和 tt 的长度。

每个测试用例的接下来两行分别包含二进制字符串 ss 和 tt。

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

输出格式

For each test case, if it is impossible to achieve the goal, print −1-1.

Otherwise, the first line should contain an integer kk (0≤k≤2n0 \leq k \leq 2n) — the number of operations.

For each of the next kk lines, print two integers l,rl, r (1≤l<r≤n1 \leq l \lt r \leq n) — the indices you choose in each operation. Note that sl,rs_{l,r} must be a palindrome at this stage.

对于每个测试用例,如果无法达成目标,则输出 −1-1。

否则,第一行应包含一个整数 kk(0≤k≤2n0 \leq k \leq 2n)—— 表示操作次数。

接下来的 kk 行中,每行输出两个整数 l,rl, r(1≤l<r≤n1 \leq l \lt r \leq n)—— 表示每次操作所选择的下标。注意:此时子串 sl,rs_{l,r} 必须是一个回文串。

输入输出样例

  • 输入#1

    3
    5
    01011
    10000
    7
    1010101
    0101010
    4
    0010
    0010

    输出#1

    2
    1 3
    3 5
    1
    1 7
    0

说明/提示

For the first test case:

  • Initially, s=01011s = \mathtt{01011} and t=10000t = \mathtt{10000}.
  • First, we choose l=1l=1 and r=3r=3. This is a valid operation since s1,3=010s_{1,3} = \mathtt{010} is a palindrome. After flipping s1,3s_{1,3}, s=10111s = \mathtt{10111}.
  • Then, we choose l=3l=3 and r=5r=5. This is a valid operation since s3,5=111s_{3,5} = \mathtt{111} is a palindrome. After flipping s3,5s_{3,5}, s=10000s = \mathtt{10000}.
  • Now, ss is equal to tt.

For the second test case:

  • Initially, s=1010101s = \mathtt{1010101} and t=0101010t = \mathtt{0101010}.
  • First, we choose l=1l=1 and r=7r=7. This is a valid operation since s1,7=1010101s_{1,7} = \mathtt{1010101} is a palindrome. After flipping s1,7s_{1,7}, s=0101010s = \mathtt{0101010}.
  • Now, ss is equal to tt.

For the third test case:

  • Initially, ss is equal to tt. No operations are needed.

对于第一个测试用例:

  • 初始时,s=01011s = \mathtt{01011} 且 t=10000t = \mathtt{10000}。
  • 首先,我们选择 l=1l=1 和 r=3r=3。该操作合法,因为子串 s1,3=010s_{1,3} = \mathtt{010} 是一个回文串。翻转 s1,3s_{1,3} 后,s=10111s = \mathtt{10111}。
  • 接着,我们选择 l=3l=3 和 r=5r=5。该操作合法,因为子串 s3,5=111s_{3,5} = \mathtt{111} 是一个回文串。翻转 s3,5s_{3,5} 后,s=10000s = \mathtt{10000}。
  • 此时,ss 已等于 tt。

对于第二个测试用例:

  • 初始时,s=1010101s = \mathtt{1010101} 且 t=0101010t = \mathtt{0101010}。
  • 首先,我们选择 l=1l=1 和 r=7r=7。该操作合法,因为子串 s1,7=1010101s_{1,7} = \mathtt{1010101} 是一个回文串。翻转 s1,7s_{1,7} 后,s=0101010s = \mathtt{0101010}。
  • 此时,ss 已等于 tt。

对于第三个测试用例:

  • 初始时,ss 已等于 tt。无需任何操作。

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

首页