第 77 场 AtCoder Grand Contest
A
Reverse A…B
AtCoder
⌀ 2512
原题
←B →

Reverse A…BAI (CI翻译)

Problem Statement

You are given strings $X$ and $Y$ of length $N$ consisting of A and B. Let $X_i$ denote the $i$-th character of $X$.

Determine whether it is possible to make $X$ equal to $Y$ by performing the following operation between $0$ and $\left\lfloor \frac{N}{2} \right\rfloor$ times, inclusive.

  • Choose a pair of integers $(l, r)$ such that $1\leq l < r \leq N$, $X_l=$ A, and $X_r=$ B. Reverse the elements of $X$ from the $l$-th to the $r$-th position. Here, "reversing the elements of $X$ from the $l$-th to the $r$-th position" means simultaneously replacing $X_l,X_{l+1},\ldots,X_{r-1},X_r$ with $X_r,X_{r-1},\ldots,X_{l+1},X_l$.

If it is possible to make $X$ equal to $Y$, output one such sequence of operations.

$T$ test cases are given; solve each of them.

Constraints

  • $1 \leq T \leq 2.5\times 10^5$
  • $2 \leq N \leq 5\times 10^5$
  • The sum of $N$ over all test cases is at most $5\times 10^5$.
  • $X$ and $Y$ are strings of length $N$ consisting of A and B.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
X
Y

Output

Output the answers for $\mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T$ in this order in the following format.

If it is impossible to make $X$ equal to $Y$ using between $0$ and $\left\lfloor \frac{N}{2} \right\rfloor$ operations, inclusive, output No.

If it is possible, output a sequence of operations in the following format:

Yes
K
l_1 r_1
l_2 r_2
\vdots
l_K r_K

Here $K$ is the number of operations, and $l_i, r_i$ are the integers $l, r$ chosen in the $i$-th operation. These must satisfy the following:

  • $0 \leq K \leq \left\lfloor \frac{N}{2} \right\rfloor$
  • $1 \leq l_i < r_i \leq N$

If there are multiple valid sequences of operations, any of them will be accepted.

{"titleZh":"反转 A…B","bodyZh":"## 题目描述\n\n给定两个长度为 $N$、由 A 和 B 组成的字符串 $X$ 和 $Y$。用 $X_i$ 表示 $X$ 的第 $i$ 个字符。\n\n请判断是否可以通过执行以下操作 $0$ 至 $\left\\lfloor \\frac{N}{2} \\right\\rfloor$ 次(包含边界)使 $X$ 变为 $Y$。\n\n- 选择一对整数 $(l, r)$,满足 $1\\leq l < r \\leq N$、$X_l=$ A 且 $X_r=$ B。将 $X$ 的第 $l$ 个至第 $r$ 个位置上的元素反转。这里,“将 $X$ 的第 $l$ 个至第 $r$ 个位置上的元素反转”是指同时将 $X_l,X_{l+1},\\ldots,X_{r-1},X_r$ 替换为 $X_r,X_{r-1},\\ldots,X_{l+1},X_l$。\n\n如果可以使 $X$ 变为 $Y$,请输出一种操作序列。\n\n共有 $T$ 个测试用例,请分别求解。\n\n## 约束条件\n\n- $1 \\leq T \\leq 2.5\\times 10^5$\n- $2 \\leq N \\leq 5\\times 10^5$\n- 所有测试用例的 $N$ 之和不超过 $5\\times 10^5$。\n- $X$ 和 $Y$ 是长度为 $N$、由 A 和 B 组成的字符串。\n- 所有输入值均为整数。\n\n## 输入格式\n\n输入从标准输入给出,格式如下:\n\n\nT\n\\mathrm{case}_1\n\\mathrm{case}_2\n\\vdots\n\\mathrm{case}_T\n`\n\n每个测试用例的输入格式如下:\n\n`\nN\nX\nY\n`\n\n## 输出格式\n\n按以下格式依次输出 $\\mathrm{case}_1,\\mathrm{case}_2,\\ldots,\\mathrm{case}_T$ 的答案。\n\n如果无法通过执行 $0$ 至 $\left\\lfloor \\frac{N}{2} \\right\\rfloor$ 次操作(包含边界)使 $X$ 变为 $Y$,输出 No。\n\n如果可以,按以下格式输出一种操作序列:\n\n`\nYes\nK\nl_1 r_1\nl_2 r_2\n\\vdots\nl_K r_K\n\n\n其中 $K$ 为操作次数,$l_i, r_i$ 是第 $i$ 次操作中选择的整数 $l, r$。它们必须满足以下条件:\n\n- $0 \\leq K \\leq \\left\\lfloor \\frac{N}{2} \\right\\rfloor$\n- $1 \\leq l_i < r_i \\leq N$\n\n如果存在多种有效的操作序列,输出其中任意一种即可。"}AI (CI翻译)

样例

Input #1
3
5
ABAAB
ABBAA
4
AAAA
BBBB
10
BABBAABBAA
BBBBAAABAA
Output #1
Yes
1
3 5
No
Yes
3
2 7
4 6
3 5
In the first test case, $X_3=$ `A` and $X_5=$ `B`, so we can perform the operation of reversing the elements of $X$ from the $3$-rd to the $5$-th position. After this operation, $X$ is `ABBAA`, which equals $Y$. In the second test case, it is impossible to make $X$ equal to $Y$ regardless of the operations performed.
题目信息
题号
agc077_a
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。