CF607C.Marbles
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
In the spirit of the holidays, Saitama has given Genos two grid paths of length n (a weird gift even by Saitama's standards). A grid path is an ordered sequence of neighbouring squares in an infinite grid. Two squares are neighbouring if they share a side.
One example of a grid path is (0, 0) → (0, 1) → (0, 2) → (1, 2) → (1, 1) → (0, 1) → ( - 1, 1). Note that squares in this sequence might be repeated, i.e. path has self intersections.
Movement within a grid path is restricted to adjacent squares within the sequence. That is, from the i-th square, one can only move to the (i - 1)-th or (i + 1)-th squares of this path. Note that there is only a single valid move from the first and last squares of a grid path. Also note, that even if there is some j-th square of the path that coincides with the i-th square, only moves to (i - 1)-th and (i + 1)-th squares are available. For example, from the second square in the above sequence, one can only move to either the first or third squares.
To ensure that movement is not ambiguous, the two grid paths will not have an alternating sequence of three squares. For example, a contiguous subsequence (0, 0) → (0, 1) → (0, 0) cannot occur in a valid grid path.
One marble is placed on the first square of each grid path. Genos wants to get both marbles to the last square of each grid path. However, there is a catch. Whenever he moves one marble, the other marble will copy its movement if possible. For instance, if one marble moves east, then the other marble will try and move east as well. By try, we mean if moving east is a valid move, then the marble will move east.
Moving north increases the second coordinate by 1, while moving south decreases it by 1. Similarly, moving east increases first coordinate by 1, while moving west decreases it.
Given these two valid grid paths, Genos wants to know if it is possible to move both marbles to the ends of their respective paths. That is, if it is possible to move the marbles such that both marbles rest on the last square of their respective paths.
本着节日精神,埼玉送给杰诺斯两条长度为 n 的网格路径(即使以埼玉的标准来看,这也是一种奇怪的礼物)。网格路径是无限网格中一系列相邻格子的有序序列。若两个格子有一条公共边,则称它们相邻。
一个网格路径的例子是:(0, 0) → (0, 1) → (0, 2) → (1, 2) → (1, 1) → (0, 1) → (−1, 1)。注意该序列中的格子可能重复出现,即路径可能存在自交。
在网格路径内移动仅限于序列中相邻的格子。也就是说,从第 i 个格子出发,只能移动到该路径的第 (i−1) 个或第 (i+1) 个格子。注意:在网格路径的第一个和最后一个格子处,仅存在唯一一个合法移动方向。此外,即使路径中存在某个第 j 个格子与第 i 个格子重合,也只允许向第 (i−1) 个和第 (i+1) 个格子移动。例如,在上述序列中,从第二个格子出发,只能移动到第一个或第三个格子。
为确保移动不产生歧义,这两条网格路径均不会包含交替出现的三个格子构成的子序列。例如,连续子序列 (0, 0) → (0, 1) → (0, 0) 在合法的网格路径中不会出现。
每个网格路径的第一个格子上各放置一颗弹珠。杰诺斯希望将两颗弹珠分别移动至各自路径的最后一个格子。但有一个限制条件:每当他移动其中一颗弹珠时,另一颗弹珠会尽可能地模仿该次移动。例如,若一颗弹珠向东移动,则另一颗弹珠也会尝试向东移动;所谓“尝试”,是指当向东移动是合法操作时,该弹珠便会实际执行向东移动。
向北移动使纵坐标增加 1,向南移动使其减少 1;类似地,向东移动使横坐标增加 1,向西移动使其减少 1。
给定这两条合法的网格路径,杰诺斯想知道:是否可能将两颗弹珠都移动至各自路径的终点?即,是否存在一种移动方式,使得两颗弹珠最终均停留在各自路径的最后一个格子上?
输入格式
The first line of the input contains a single integer n (2 ≤ n ≤ 1 000 000) — the length of the paths.
The second line of the input contains a string consisting of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') — the first grid path. The characters can be thought of as the sequence of moves needed to traverse the grid path. For example, the example path in the problem statement can be expressed by the string "NNESWW".
The third line of the input contains a string of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') — the second grid path.
输入的第一行包含一个整数 n(2≤n≤1000000)—— 路径的长度。
输入的第二行包含一个由 n−1 个字符组成的字符串(每个字符为 'N'、'E'、'S' 或 'W' 中的一个)—— 第一条网格路径。这些字符可视为遍历该网格路径所需的一系列移动操作。例如,题目描述中的示例路径可用字符串 "NNESWW" 表示。
输入的第三行包含一个由 n−1 个字符组成的字符串(每个字符为 'N'、'E'、'S' 或 'W' 中的一个)—— 第二条网格路径。
输出格式
Print "YES" (without quotes) if it is possible for both marbles to be at the end position at the same time. Print "NO" (without quotes) otherwise. In both cases, the answer is case-insensitive.
如果两个弹珠能同时到达终点位置,则输出 "YES"(不带引号);否则输出 "NO"(不带引号)。两种情况下,答案均不区分大小写。
输入输出样例
输入#1
7 NNESWW SWSWSW
输出#1
YES
输入#2
3 NN SS
输出#2
NO
说明/提示
In the first sample, the first grid path is the one described in the statement. Moreover, the following sequence of moves will get both marbles to the end: NNESWWSWSW.
In the second sample, no sequence of moves can get both marbles to the end.
在第一个样例中,第一条网格路径即为题目描述中所给出的路径。此外,以下移动序列可使两个弹珠均到达终点:NNESWWSWSW。
在第二个样例中,不存在任何移动序列能使两个弹珠均到达终点。
输入解题思路,AI测评打分。不知道怎么写?