Robot on the Board 1
Contest: Codeforces Round 753 (Div. 3)
Difficulty rating: 1600
Tags: implementation

The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.

The robot is able to move from the current cell to one of the four cells adjacent by side.

The sequence of commands $$$s$$$ executed by the robot is given. Each command is denoted by one of the symbols 'L', 'R', 'D' or 'U', and triggers the movement to left, right, down or up, respectively.

The robot can start its movement in any cell. The robot executes the commands starting from the first one, strictly in the order in which they are listed in $$$s$$$. If the robot moves beyond the edge of the board, it falls and breaks. A command that causes the robot to break is not considered successfully executed.

The robot's task is to execute as many commands as possible without falling off the board. For example, on board $$$3 \times 3$$$, if the robot starts a sequence of actions $$$s=$$$"RRDLUU" ("right", "right", "down", "left", "up", "up") from the central cell, the robot will perform one command, then the next command will force him to cross the edge. If the robot starts moving from the cell $$$(2, 1)$$$ (second row, first column) then all commands will be executed successfully and the robot will stop at the cell $$$(1, 2)$$$ (first row, second column).

The robot starts from cell $$$(2, 1)$$$ (second row, first column). It moves right, right, down, left, up, and up. In this case it ends in the cell $$$(1, 2)$$$ (first row, second column).

Determine the cell from which the robot should start its movement in order to execute as many commands as possible.

Input format:
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.

The next $$$2t$$$ lines contain descriptions of the test cases.

In the description of each test case, the first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^6$$$) — the height and width of the field that the robot is located on. The second line of the description is a string $$$s$$$ consisting solely of characters 'L', 'R', 'D' and 'U' — the sequence of commands the robot executes. The string has a length from $$$1$$$ to $$$10^6$$$ commands.

It is guaranteed that the total length of $$$s$$$ over all test cases does not exceed $$$10^6$$$.

Output format:
Print $$$t$$$ lines, each of which contains the answer to the corresponding test case. The answer to the test case are two integers $$$r$$$ ($$$1 \leq r \leq n$$$) and $$$c$$$ ($$$1 \leq c \leq m$$$), separated by a space — the coordinates of the cell (row number and column number) from which the robot should start moving to perform as many commands as possible.

If there are several such cells, you may output any of them.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Example input:
4
1 1
L
1 2
L
3 3
RRDLUU
4 3
LUURRDDLLLUU

Example output:
1 1
1 2
2 1
3 2

Editorial:
Let's look at the answer $$$(r, c)$$$. Let's see how many commands the robot can execute. Since the robot breaks as soon as goes outside the field, if any command causes it to break, it either leads to its total shift relative to $$$(r, c)$$$ of exactly $$$c$$$ to the left or exactly $$$m - c + 1$$$ to the right, or, similarly, of exactly $$$r$$$ up or exactly $$$n - r + 1$$$ down.
Denote by $$$d[\leftrightarrow]$$$ and $$$d[\updownarrow]$$$ the sum of the maximum positive (right, down) and maximum negative (left, up) shifts in the corresponding direction. By adding up the above constraints, we get the fact that the robot will not fall off the board only if $$$d[\leftrightarrow] \leqslant (c - 1) + ((m - c + 1) - 1) = m - 1$$$ and $$$d[\updownarrow] \leqslant (r - 1) + ((n - r + 1) - 1) = n - 1$$$.
Note that the reverse is also true: if both these conditions are satisfied, then starting from the point $$$(\max[\leftarrow] + 1, \max[\uparrow] + 1)$$$ where $$$\max[\mathtt{dir}]$$$ is the maximum shift along the direction $$$\mathtt{dir}$$$, the robot will not pass any of the board's edges. Thus, it is sufficient to find the number of commands which, when executed, hold the following invariant: $$$$$$d[\leftrightarrow] \leqslant m - 1 \land d[\updownarrow] \leqslant n - 1$$$$$$
The horizontal shift can be calculated as the difference between the number of letters 'R' and the number of letters 'L' encountered. Similarly, the vertical shift — as the difference of the numbers of 'D' and 'U'. Let's iterate over the sequence of commands, maintaining relevant values of $$$\max[\mathtt{dir}]$$$ for all $$$\mathtt{dir} \in [\leftarrow, \rightarrow, \uparrow, \downarrow]$$$. After executing each command, if the robot goes farther in some direction than ever before, we update the corresponding $$$\max$$$.
Either we reach the end of $$$s$$$, or we meet a command after which either $$$d[\leftrightarrow] = \max[\leftarrow] + \max[\rightarrow]$$$ becomes equal to $$$m$$$, or $$$d[\updownarrow] = \max[\uparrow] + \max[\downarrow]$$$ becomes equal to $$$n$$$ and the robot breaks, so the previous command was the last one successfully executed. The possible answer is $$$(\max[\leftarrow] + 1, \max[\uparrow] + 1)$$$, where the $$$\max$$$ values are calculated one command before the robot broke.