Codeforces Round 1067 (Div. 2)
D
Palindrome Flipping
Codeforces
2000
原题
← CE →
翻译中…

You are given two binary strings $s$ and $t$, each of the same length $n$. You are allowed to perform the following operation:

翻译中…
  • Pick indices $l$, $r$ ($1 \le \color{red}{l \lt r} \le n$) such that substring $s_{l,r}$ is a palindrome and flip all bits in substring $s_{l,r}$.
翻译中…

The goal is to finally make $s$ equal to $t$, performing any of the above operations at most $2n$ times (possibly none).

翻译中…

A substring $s_{l,r}$ of a string $s$ is the contiguous sequence of characters starting from index $l$ and ending at index $r$ (both inclusive), where $1 \leq l \lt r \leq |s|$. Here $|s|$ denotes the length of the string $s$.

翻译中…

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.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each test case contains an integer $n$ ($4 \le n \le 100$) — the length of the strings $s$ and $t$.

翻译中…

The next two lines of each test case contain the binary strings $s$ and $t$, respectively.

翻译中…

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

翻译中…

Output

翻译中…

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

翻译中…

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

翻译中…

For each of the next $k$ lines, print two integers $l, r$ ($1 \leq l \lt r \leq n$) — the indices you choose in each operation. Note that $s_{l,r}$ must be a palindrome at this stage.

翻译中…

Note

翻译中…

For the first test case:

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

For the second test case:

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

For the third test case:

翻译中…
  • Initially, $s$ is equal to $t$. No operations are needed.
翻译中…

样例

Input #1
3
5
01011
10000
7
1010101
0101010
4
0010
0010
Output #1
2
1 3
3 5
1
1 7
0
InputCopy OutputCopy
题目信息
题号
2158D
时间限制
2 s
内存限制
256 MB
标签
暴力
构造
图论
模拟
最短路
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。