您的位置:首页 > 其它

Codeforces 607C:Marbles 纯脑洞。。。。

2015-12-27 17:38 411 查看
C. Marbles

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Input

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.

Output

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.

Sample test(s)

input
7
NNESWW
SWSWSW


output
YES


input
3
NN
SS


output
NO


Note

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.

题意我就读了好久。。。最后明白过来题意是给出了两个坑道,由“NSWE”表示上下左右。然后有两个球在各自坑道的起点。之后由你对两个小球做相同的操作,想让小球往上走就往上走,想往下走就往下走。但是要保证两个小球保持一致的动作,其中一个撞墙的话不用管。问能不能保证两个小球都能从起点到达终点。

题解给出的结论是如果两个坑道的后缀是完全相反的序列(不一定最后一步反向,最后两步反向,最后三步反向都是),那么就肯定到达不了。如果不是那么就能够到达。后来想了一想,自己把自己说服了的理由是,不管怎么样,两个小球都到达终点的做法是先把一个搞到终点,然后再去搞另一个。那么你这时如果序列相反,就相当于坑道的结构是相同的,但是一个小球在起点,一个小球在终点。。。

这样你怎么到达嘛,一个小球好不容易跑到了终点了,然后发现另一个小球回到了起点了。。。

所以说将一个序列逆序一下之后,再transfer一下,判断其前缀是否等于后缀。

KMP或者哈希都可以。

代码:

#pragma warning(disable:4996)  
#include <iostream>  
#include <algorithm>  
#include <cmath>  
#include <vector>  
#include <string>  
#include <cstring>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x3fffffff

const int maxn = 2e6 + 5;

int n, len;
int nex[maxn];
string a, b;
string all;

char trans(char x)
{
	if (x == 'N')
	{
		return 'S';
	}
	else if (x == 'S')
	{
		return 'N';
	}
	else if (x == 'W')
	{
		return 'E';
	}
	else if (x == 'E')
	{
		return 'W';
	}
}

void kmp(string s)
{	
	int i, j;
	len = s.length();

	nex[0] = -1;
	j = -1;
	for (i = 1; i < len; i++)
	{
		while (j != -1 && s[i] != s[j + 1])
		{
			j = nex[j];
		}
		if (s[i] == s[j + 1])
			j++;
		nex[i] = j;
	}
}

void input()
{
	cin >> n >> a >> b;
}

void solve()
{
	n--;
	
	reverse(b.begin(), b.end());
	for(char &c : b)
		c = trans(c);
	all = b + ' ' + a;
	kmp(all);
	puts(nex[len - 1] == -1 ? "YES" : "NO");

}

int main()
{
	//freopen("i.txt", "r", stdin);
	//freopen("o.txt", "w", stdout);

	input();
	solve();

	//system("pause");
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: