第 1130 场 Codeforces Round(Div. 2)
E
内存单元拦截
Memory Cell Interception
Codeforces
未评级
原题
← DF →

内存单元拦截AI (CI翻译)

There are two threads, $\texttt{A}$ and $\texttt{B}$, and one shared memory cell. Initially, the memory cell contains $0$.

有两个线程 $\texttt{A}$ 和 $\texttt{B}$,以及一个共享内存单元。初始时,内存单元中存储的值为 $0$。AI (CI翻译)

Thread $\texttt{A}$ has a fixed command sequence $s$ of length $n$, and thread $\texttt{B}$ has a fixed command sequence $t$ of length $m$. Each command is either $\texttt{READ}$, denoted by $\texttt{R}$, or $\texttt{WRITE}$, denoted by $\texttt{W}$.

线程 $\texttt{A}$ 有一个长度为 $n$ 的固定指令序列 $s$,线程 $\texttt{B}$ 有一个长度为 $m$ 的固定指令序列 $t$。每条指令要么是读指令 $\texttt{READ}$,记作 $\texttt{R}$;要么是写指令 $\texttt{WRITE}$,记作 $\texttt{W}$。AI (CI翻译)

The commands of each thread must be executed in their original order. However, commands from the two threads may be interleaved arbitrarily. In other words, the actual execution is a global sequence of $n+m$ commands obtained by merging $s$ and $t$ while preserving the order of commands inside each string.

每个线程中的指令都必须按照原有顺序执行,但两个线程的指令可以任意交错执行。换言之,实际执行的是一个由 $n+m$ 条指令组成的全局序列,该序列通过合并 $s$ 和 $t$ 得到,且保持每个字符串内部指令的相对顺序不变。AI (CI翻译)

For example, suppose $s=$ $\texttt{RWW}$ and $t=$ $\texttt{WR}$.

例如,假设 $s=$ $\texttt{RWW}$,$t=$ $\texttt{WR}$。AI (CI翻译)

  • The sequence $ s_1,t_1,s_2,t_2,s_3 $ is a valid interleaving. The corresponding global command sequence is $\texttt{RWWRW}$.
  • The sequence $ s_2,t_1,s_1,t_2,s_3 $ is not a valid interleaving, because the commands of $s$ are not executed in their original order.
  • The sequence $ s_1,t_1,s_2,t_2 $ is not a valid interleaving, because it does not execute all commands.
  • 序列 $ s_1,t_1,s_2,t_2,s_3 $ 是一种合法的交错执行顺序。对应的全局指令序列为 $\texttt{RWWRW}$。
  • 序列 $ s_2,t_1,s_1,t_2,s_3 $ 不是合法的交错执行顺序,因为 $s$ 中的指令没有按照原有顺序执行。
  • 序列 $ s_1,t_1,s_2,t_2 $ 不是合法的交错执行顺序,因为它没有执行所有指令。
AI (CI翻译)

The global execution has timestamps. Before any command is executed, the timestamp is $0$. The command executed at position $i$ in the global sequence has timestamp $i$.

全局执行过程带有时间戳。在执行任何指令之前,时间戳为 $0$。全局序列中第 $i$ 个执行的指令的时间戳为 $i$。AI (CI翻译)

  • When a $\texttt{WRITE}$ command is executed at timestamp $i$, it writes the value $i$ into the shared memory cell.
  • When a $\texttt{READ}$ command is executed, it reads the current timestamp value stored in the shared memory cell.
  • 当一条 $\texttt{WRITE}$ 指令在时间戳 $i$ 执行时,它会将值 $i$ 写入共享内存单元。
  • 当一条 $\texttt{READ}$ 指令执行时,它会读取共享内存单元中当前存储的时间戳值。
AI (CI翻译)

For every $\texttt{READ}$ command appearing in either $s$ or $t$, determine how many distinct values it can read over all valid interleavings of the two command sequences.

对于 $s$ 或 $t$ 中的每一条 $\texttt{READ}$ 指令,求在两个指令序列的所有合法交错执行顺序中,它能够读取到多少个不同的值。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多组测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 10^4$)。接下来是各组测试用例的描述。AI (CI翻译)

The first line of each test case contains two integers $n$ and $m$ ($1 \le n,m \le 2 \cdot 10^5$) — the lengths of the command sequences of threads $\texttt{A}$ and $\texttt{B}$, respectively.

每组测试用例的第一行包含两个整数 $n$ 和 $m$($1 \le n,m \le 2 \cdot 10^5$),分别表示线程 $\texttt{A}$ 和 $\texttt{B}$ 的指令序列长度。AI (CI翻译)

The second line contains a string $s$ of length $n$ — the command sequence of thread $\texttt{A}$.

第二行包含一个长度为 $n$ 的字符串 $s$,表示线程 $\texttt{A}$ 的指令序列。AI (CI翻译)

The third line contains a string $t$ of length $m$ — the command sequence of thread $\texttt{B}$.

第三行包含一个长度为 $m$ 的字符串 $t$,表示线程 $\texttt{B}$ 的指令序列。AI (CI翻译)

Each character of $s$ and $t$ is either $\texttt{R}$ or $\texttt{W}$. Each of the strings $s$ and $t$ contains at least one character $\texttt{R}$.

$s$ 和 $t$ 中的每个字符均为 $\texttt{R}$ 或 $\texttt{W}$。字符串 $s$ 和 $t$ 都至少包含一个字符 $\texttt{R}$。AI (CI翻译)

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

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$,所有测试用例的 $m$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, print two lines.

对于每组测试用例,输出两行。AI (CI翻译)

The first line contains a sequence of integers, representing the answer for each $\texttt{READ}$ instruction appearing in $s$ in order. The length of the sequence must equal the number of letters $\texttt{R}$ in $s$.

第一行输出一个整数序列,按顺序给出 $s$ 中每条 $\texttt{READ}$ 指令的答案。序列的长度必须等于 $s$ 中字母 $\texttt{R}$ 的数量。AI (CI翻译)

The second line contains a sequence of integers, representing the answer for each $\texttt{READ}$ instruction appearing in $t$ in order. The length of the sequence must equal the number of letters $\texttt{R}$ in $t$.

第二行输出一个整数序列,按顺序给出 $t$ 中每条 $\texttt{READ}$ 指令的答案。序列的长度必须等于 $t$ 中字母 $\texttt{R}$ 的数量。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first example, both $\texttt{R}$ commands can only get the value $0$ initially set in the cell, so the answers are both $1$.

在第一个样例中,两条 $\texttt{R}$ 指令都只能读取到内存单元的初始值 $0$,因此答案均为 $1$。AI (CI翻译)

In the second example, there are $3$ different interleavings (the red letter $\texttt{R}$ represents the one in $s$):

在第二个样例中,共有 $3$ 种不同的交错执行顺序(红色字母 $\texttt{R}$ 表示 $s$ 中的那条指令):AI (CI翻译)

  • $\mathtt{\color{red}{R}WR}$: the first $\texttt{R}$ reads $0$, the second $\texttt{R}$ reads $2$;
  • $\mathtt{W\color{red}{R}R}$: the first $\texttt{R}$ reads $1$, the second $\texttt{R}$ reads $1$;
  • $\mathtt{WR\color{red}{R}}$: the first $\texttt{R}$ reads $1$, the second $\texttt{R}$ reads $1$;
  • $\mathtt{\color{red}{R}WR}$:第一条 $\texttt{R}$ 读取到 $0$,第二条 $\texttt{R}$ 读取到 $2$;
  • $\mathtt{W\color{red}{R}R}$:第一条 $\texttt{R}$ 读取到 $1$,第二条 $\texttt{R}$ 读取到 $1$;
  • $\mathtt{WR\color{red}{R}}$:第一条 $\texttt{R}$ 读取到 $1$,第二条 $\texttt{R}$ 读取到 $1$;
AI (CI翻译)

Above all, both $\texttt{R}$ commands might see $2$ distinct values.

综上,两条 $\texttt{R}$ 指令都可能读取到 $2$ 个不同的值。AI (CI翻译)

样例

Input #1
3
1 1
R
R
1 2
R
WR
3 3
WRW
RWR
Output #1
1
1
2
2
4
3 4
InputCopy OutputCopy
题目信息
题号
2272E
时间限制
2 s
内存限制
256 MB
标签
贪心

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