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.
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.
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.
7
NNESWW
SWSWSW
YES
3
NN
SS
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.
【题解】
题意是说给你两条道路,每条道路上有一个球,你每次可以让两个球同时朝某个方向走一步,
如果某个球不能朝某个方向走那它就不走。问你是否可以让两个球到达终点。
有一个结论是如果一条道路的反转串(即'W'<->'E', 'N'<->'S',并左右倒转)的前缀与第二条道路原串的
后缀相同,则不可走到。
画画图,显然法证明吧。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #define max(a, b) ((a) > (b) ? (a) : (b)) 6 #define min(a, b) ((a) < (b) ? (a) : (b)) 7 inline void swap(char &a, char &b) 8 { 9 int tmp = a;a = b;b = tmp; 10 } 11 inline void read(int &x) 12 { 13 x = 0;char ch = getchar(), c = ch; 14 while(ch < '0' || ch > '9')c = ch, ch = getchar(); 15 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar(); 16 if(c == '-')x = -x; 17 } 18 19 const int MAXN = 1000000 + 10; 20 21 int n, nxt[MAXN]; 22 char s1[MAXN], s2[MAXN]; 23 24 inline void yuchuli() 25 { 26 nxt[0] = -1; 27 for(register int i = 1, j = -1;i < n;++ i) 28 { 29 while(j >= 0 && s1[j + 1] != s1[i])j = nxt[j]; 30 if(s1[j + 1] == s1[i])++ j; 31 nxt[i] = j; 32 } 33 } 34 35 int KMP() 36 { 37 for(register int i = 0, j = -1;i < n;++ i) 38 { 39 while(j >= 0 && s1[j + 1] != s2[i]) j = nxt[j]; 40 if(s1[j + 1] == s2[i]) ++ j; 41 if(i == n - 1 && j >= 0)return 1; 42 43 } 44 return 0; 45 } 46 47 int main() 48 { 49 //freopen("data.txt", "r", stdin); 50 read(n); 51 scanf("%s", s1);scanf("%s", s2); 52 -- n; 53 for(register int i = 0;i < n;++ i) 54 { 55 if(s1[i] == 'N')s1[i] = 'S'; 56 else if(s1[i] == 'S')s1[i] = 'N'; 57 else if(s1[i] == 'W')s1[i] = 'E'; 58 else s1[i] = 'W'; 59 } 60 for(register int i = n/2 - 1;i >= 0;-- i) 61 swap(s1[i], s1[n - i - 1]); 62 yuchuli(); 63 if(KMP()) 64 printf("NO"); 65 else 66 printf("YES"); 67 return 0; 68 }