Codeforces Round #336 (Div. 1) C. Marbles

来源:互联网 发布:fm 迭戈科斯塔数据 编辑:程序博客网 时间:2024/06/11 09:05

题意很好理解

方法是从codeforce的博客上学来的点击打开链接

也很容易证明

然后就是怎么求串a的后缀和串b的前缀是否有相等的

只用kmp扫一遍就好了

真是机智啊!!!

自己想不到


#include <bits/stdc++.h>using namespace std;#define REP(i, a, b) for (int i = (a), _end_ = (b); i < _end_; ++i)template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }typedef long long LL;const int oo = 0x3f3f3f3f;const int maxn = 1000000;int n;char a[maxn + 5], b[maxn + 5];int nxt[maxn + 5];int main(){scanf("%*d");scanf("%s%s", a, b);n = strlen(a);REP(i, 0, n){if (a[i] == 'N') a[i] = 'S';else if (a[i] == 'S') a[i] = 'N';else if (a[i] == 'E') a[i] = 'W';else if (a[i] == 'W') a[i] = 'E';}reverse(b, b + n);//倒置函数  nxt[0] = 0, nxt[1] = 0;int i = 1, j = 0;while (i < n){while (j && b[i] != b[j]) j = nxt[j];if (b[i] == b[j]) ++j;++i;nxt[i] = j;}int cur = 0;REP(i, 0, n){while (cur && a[i] != b[cur]) cur = nxt[cur];if (a[i] == b[cur]) ++cur;}puts(cur ? "NO" : "YES");return 0;}


0 0
原创粉丝点击