要点
- 做法是删去连续的k个0或k个1,连消、消消乐的那种,网上博主用个栈(O(n))就很优秀地操作了这个过程
- 原因是有性质:比如k=3,101000贪心地翻就能翻成000101,所以连续的k个可以都挪到后面去
/*GYM 101981 E*/
#include <cstdio>
#include <string>
using namespace std;
const int maxn = 1e6 + 5;
int n, k;
char s[maxn], t[maxn];
pair<int, int> q[maxn];
string solve(char *s) {
int tail = 0;
q[tail] = {-1, 0};
for (int i = 1; i <= n; i++) {
q[++tail].first = s[i];
q[tail].second = (s[i] == q[tail - 1].first) ? q[tail - 1].second + 1 : 1;
if (q[tail].second == k) tail -= k;
}
string res = "";
for (int i = 1; i <= tail; i++)
res += q[i].first;
return res;
}
int main() {
scanf("%d %d %s %s", &n, &k, s + 1, t + 1);
puts(solve(s) == solve(t) ? "Yes" : "No");
}