102912: [Atcoder]ABC291 C - LRUD Instructions 2

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:14 Solved:0

Description

Score : $300$ points

Problem Statement

Takahashi is on a two-dimensional plane. Starting from the origin, he made $M$ moves.

The $N$ moves are represented by a string of length $N$ as described below:

  • Takahashi's coordinates after the $i$-th move are:

    • $(x+1,y)$ if the $i$-th character of $S$ is R;
    • $(x-1,y)$ if the $i$-th character of $S$ is L;
    • $(x,y+1)$ if the $i$-th character of $S$ is U; and
    • $(x,y-1)$ if the $i$-th character of $S$ is D,

    where $(x,y)$ is his coordinates before the move.

Determine if Takahashi visited the same coordinates multiple times in the course of the $N$ moves (including the starting and ending points).

Constraints

  • $1 \leq N \leq 2\times 10^5$
  • $N$ is an integer.
  • $S$ is a string of length $N$ consisting of R, L, U, and D.

Input

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

$N$
$S$

Output

Print Yes if Takahashi visited the same coordinates multiple times in the course of the $N$ moves; print No otherwise.


Sample Input 1

5
RLURU

Sample Output 1

Yes

Takahashi's coordinates change as follows: $(0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2)$.


Sample Input 2

20
URDDLLUUURRRDDDDLLLL

Sample Output 2

No

Input

题意翻译

高桥从 $(0,0)$ 的位置开始走,将移动前的坐标记为 $(x,y)$,对于字符串: - 如果第 $i$ 个字母是 `R` 移动到 $(x+1,y)$ 的位置。 - 如果第 $i$ 个字母是 `L` 移动到 $(x-1,y)$ 的位置。 - 如果第 $i$ 个字母是 `U` 移动到 $(x,y + 1)$ 的位置。 - 如果第 $i$ 个字母是 `D` 移动到 $(x,y - 1)$ 的位置。 如果移动过程中重复经过了同一个位置(包括起点和终点)则输出 `Yes`,否则输出 `No`。 第一行输入一个 $N$,第二行输入一个长度为 $N$ 的字符串。

Output

得分:300分 部分 问题描述 Takahashi 在二维平面上。从原点开始,他做了 $N$ 次移动。 这 $N$ 次移动可以用以下方式表示的长度为 $N$ 的字符串表示: - 如果 $S$ 的第 $i$ 个字符是 R,则 Takahashi 在第 $i$ 次移动后的坐标为 $(x+1,y)$; - 如果 $S$ 的第 $i$ 个字符是 L,则 Takahashi 在第 $i$ 次移动后的坐标为 $(x-1,y)$; - 如果 $S$ 的第 $i$ 个字符是 U,则 Takahashi 在第 $i$ 次移动后的坐标为 $(x,y+1)$; - 如果 $S$ 的第 $i$ 个字符是 D,则 Takahashi 在第 $i$ 次移动后的坐标为 $(x,y-1)$, 其中 $(x,y)$ 是他移动前的坐标。 确定 Takahashi 在 $N$ 次移动过程中(包括起始点和结束点)是否多次访问了相同的坐标。 部分 约束 - $1 \leq N \leq 2\times 10^5$ - $N$ 是一个整数。 - $S$ 是一个长度为 $N$ 的字符串,由 RLUD 组成。 输入格式 输入从标准输入给出,格式如下: ``` $N$ $S$ ``` 输出格式 如果 Takahashi 在 $N$ 次移动过程中多次访问了相同的坐标,则打印 Yes;否则打印 No。 样例输入 1 ``` 5 RLURU ``` 样例输出 1 ``` Yes ``` Takahashi 的坐标按以下方式变化:$(0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2)$。 样例输入 2 ``` 20 URDDLLUUURRRDDDDLLLL ``` 样例输出 2 ``` No ```

HINT

移动n次,判断是否会走到同一个位置两次?

加入题单

算法标签: