AtCoder Regular Contest 197 (Div. 2)
A
Union of Grid Paths
AtCoder
⌀ 860
原题
←B →
翻译中…

Problem Statement

翻译中…

There is an $H\times W$ grid of cells. Let $(h,w)$ denote the cell at the $h$-th row from the top and the $w$-th column from the left. Furthermore, you are given a string $S = S_1\cdots S_{H+W-2}$ of length $H+W-2$ consisting of D, R, and ?.

翻译中…

Initially, all cells are painted white. You may perform the following operation, which consists of three steps, any number of times:

翻译中…
  1. Choose a string $X = X_1\cdots X_{H+W-2}$ of length $H+W-2$ satisfying all of the following.
  • $X$ consists of exactly $H-1$ Ds and $W-1$ Rs.
    • For each $1 \le i \le H+W-2$, if $S_i$ is D then $X_i$ must also be D.
    • For each $1 \le i \le H+W-2$, if $S_i$ is R then $X_i$ must also be R.
  1. Stand on cell $(1,1)$. Then for $i=1,2,\ldots$ in order, move one cell in the direction indicated by $X_i$: if $X_i$ is D, move down one cell; if $X_i$ is R, move right one cell. It can be shown that if $X$ satisfies the conditions in step 1, the destination cell always exists within the grid.
  2. For every cell you visited in step 2 (including the starting and ending cells), if the cell is currently white, paint it black.
翻译中…

Find the maximum possible number of cells that can be painted black in total.

翻译中…

There are $T$ test cases; solve each one.

翻译中…

Constraints

翻译中…
  • $1\le T\le 2\times 10^5$
  • $2\le H,W\le 2\times 10^5$
  • $T,H,W$ are integers.
  • $S$ is a string of length $H+W-2$ consisting of D, R, and ?.
  • The number of Ds in $S$ is at most $H-1$.
  • The number of Rs in $S$ is at most $W-1$.
  • The sum of $H+W$ over all test cases is at most $4\times 10^5$.
翻译中…

Input

翻译中…

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

翻译中…

T
\text{case}_1
\vdots
\text{case}_T

翻译中…

Each case is given in the following format:

翻译中…

H W
S

翻译中…

Output

翻译中…

Print $T$ lines. The $i$-th line should contain the maximum number of cells that can be painted black for the $i$-th test case.

翻译中…

样例

Input #1
4
4 5
D?DRR?R
4 5
DDRRDRR
4 5
???????
2 2
DR
Output #1
12
8
20
3
For the first test case, by choosing $X$ as `DRDRRDR` in the first operation and `DDDRRRR` in the second operation, you can paint $12$ cells black. ![image](https://img.atcoder.jp/arc197/fe14a42d236585f23bf6e59480bb45ac.png)
题目信息
题号
arc197_a
时间限制
2 s
内存限制
1024 MB

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