E. 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
  1. 7
    NNESWW
    SWSWSW
output
  1. YES
input
  1. 3
    NN
    SS
output
  1. 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.

这题模拟搜索的方法应该不难想到,复杂度O(n^2logn),然而太慢。

考虑一种特例,即两个串表示的图形完全相同,只不过起始点和终点相反。在这种情况下可以断定是不能将两个球同时移动到终点的。

否则,假设两球能够被同时移动到终点,那么我们将两张图覆盖在一起,考虑移动过程的逆序,A球在A的终点,B的起点,B球在B的终点,A的起点。

现在试图将A移回A的起点,即B现在的位置,同时试图将B移到A现在的位置,由于共用一张地图,它们一定会在某点相遇,那么它们以后的运动轨迹

必然完全相同,因此当A回到A的起点是,B也到A的起点。

实际上,两个球不能同时抵达终点当且仅当两串存在满足上述条件的后缀子串。

充分性上面已经给出了说明,下面说明必要性。

考虑一种情形,将球a在从起点移动到终点,此时b停留在终点,假设a球向终点靠近了k步,那么b球在最坏情况下后退的步数显然为k。

在这种情形下,必然有两串有后缀子串满足上述条件,因为两球移动轨迹相同,方向相反,若不存在符合要求的后缀子串,b至多退k-1步。

a、b交替如此移动,总的接近终点的距离严格递增,必然能同时到达终点。

可以用hash或kmp在线性时间内判断子串的存在性。

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. const int maxn = 1e6 + ;
  6. char s[maxn], t[maxn];
  7. int n;
  8. int f[];
  9. int fail[maxn];
  10.  
  11. int solve(){
  12. f['W'] = 'E', f['E'] = 'W', f['N'] = 'S', f['S'] = 'N';
  13. int len = n - ;
  14. for(int i = ; i < len; i++) s[i] = f[s[i]];
  15. int mid = len >> ;
  16. for(int i = ; i < mid; i++) swap(s[i], s[n - - i]);
  17. fail[] = fail[] = ;
  18. for(int i = ; i < len; i++){
  19. int j = fail[i];
  20. while(j && s[j] != s[i]) j = fail[j];
  21. fail[i + ] = s[j] == s[i] ? j + : ;
  22. }
  23. int p = ;
  24. for(int i = ; i < len; i++){
  25. while(p && t[i] != s[p]) p = fail[p];
  26. p = t[i] == s[p] ? + p : ;
  27. }
  28. return !p;
  29. }
  30.  
  31. int main(){
  32. //freopen("in.txt", "r", stdin);
  33. while(~scanf("%d", &n)){
  34. scanf("%s%s", s, t);
  35. int ans = solve();
  36. puts(ans ? "YES" : "NO");
  37. }
  38. return ;
  39. }

Codeforces Round #336 Marbles的更多相关文章

  1. Codeforces Round #336 (Div. 2) D. Zuma

    Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...

  2. Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】

    A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:sta ...

  3. Codeforces Round #336 Hamming Distance Sum

    题目: http://codeforces.com/contest/608/problem/B 字符串a和字符串b进行比较,以题目中的第一个样例为例,我刚开始的想法是拿01与00.01.11.11从左 ...

  4. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

  5. Codeforces Round #336 (Div. 2) C. Chain Reaction set维护dp

    C. Chain Reaction 题目连接: http://www.codeforces.com/contest/608/problem/C Description There are n beac ...

  6. Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和

    B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...

  7. Codeforces Round #336 (Div. 2)A. Saitama Destroys Hotel 水题

    A. Saitama Destroys Hotel 题目连接: http://www.codeforces.com/contest/608/problem/A Description Saitama ...

  8. Codeforces Round #336 (Div. 2) D. Zuma(区间DP)

    题目链接:https://codeforces.com/contest/608/problem/D 题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最 ...

  9. Codeforces Round #336 (Div. 2)

    水 A - Saitama Destroys Hotel 简单的模拟,小贪心.其实只要求max (ans, t + f); #include <bits/stdc++.h> using n ...

随机推荐

  1. maven增加Spring

    对于javax.annotation,spring某些注解要用的,比如:@Resource.   使用Maven管理基本的Spring依赖关系 Spring被设计为可高度模块化的 —— 使用Sprin ...

  2. JS练习题 显示登入者相关好友

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Java基础(4):Scanner输入的典型应用

    import java.util.Scanner; /* * 功能:为指定的成绩加分,直到分数大于等于60为止 * 输出加分前的成绩和加分后的成绩,并且统计加分的次数 * 步骤: * 1.定义一个变量 ...

  5. 今天遇到的一个问题(windows的ssh客户端连接不到虚拟机Ubuntu)

    今天比较郁闷,想用windows上的ssh客户端连接虚拟机中的Ubuntu. 但是死活连不上,之前是能脸上的,所以比较郁闷. 我首先在windows上ping Ubuntu的ip地址,竟然发不了数据包 ...

  6. 浅谈js中的数据类型,使用typeof获取js数据类型

    JS中的数据类型 1):Undefined——值未定义 注:Undefined类型只有一个值,即特色的undefined.在使用var声明变量但未对其加以初始化时,这个变量的值就是undefined ...

  7. Vim篇

    Vim编辑器中的一些常用命令: 1:shift+* , 选取光标所在处的整个字符,并查找.(十分方便),快捷键gd 2:set nu , 显示各行行号,使得基于行的命令更方便. 3:shift+% , ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON DirectShow (Delphi Prism)

    zw版[转发·台湾nvp系列Delphi例程]HALCON DirectShow (Delphi Prism) namespace DirectShow_Prism;interfaceuses Sys ...

  9. zw版【转发·台湾nvp系列Delphi例程】HALCON HWindow Overlayer 1

    zw版[转发·台湾nvp系列Delphi例程]HALCON HWindow Overlayer 1 ------------------------------------HALCON HWindow ...

  10. [tp3.2.1]开启URL(重写模式),省略URL中的index.php

    重写模式(省略url中的index.php) 在apache配置文件httpd.conf中,查找 1.mod_rewrite.so, 启动此模块 2.AllowOverride , 值= All 3. ...