Problem E. Eva and Euro coins(思维)
题意:给你一个长为n的初始01串与目标01串,你可以每次对连续k个数进行翻转,如:100 -> 011,问你通过一系列的操作能否将初始串变成目标串。
思路: 将两个串中连续k个相等的字符删掉, 看最后剩下的俩串是否相等,相等则yes。
#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define up(i, a, b)for(int i = a;i <= b; i++)
#define down(i, a, b)for(int i =a;i >= b; i--)
#define inf 0x3f3f3f3f
#define int long long
using namespace std;
const int maxn = 1e6 + 700;
char a[maxn], b[maxn];
int res[maxn], now[maxn];
stack<int> staa, stab, num;
signed main()
{
int k, n;
scanf("%lld%lld", &n, &k);
scanf("%s", a);
scanf("%s", b);
if(k == 1)puts("Yes");
else{
int lena = strlen(a), lenb = strlen(b);
int i = 0;
while(i < n){
if(staa.empty() || staa.top() != a[i]){
staa.push(a[i]);num.push(1);
i++;
}
else{
if((num.top()+1) == k){
i++;
int tmp = k-1;
while(tmp--){
num.pop();staa.pop();
}
}
else if(staa.top() == a[i]){
int tmp = num.top();
num.push(tmp+1);
staa.push(a[i]);i++;
}
}
}
i = 0;
while(!num.empty())num.pop();
while(i < n){
if(stab.empty() || stab.top() != b[i]){
stab.push(b[i]);num.push(1);
i++;
}
else{
if((num.top()+1) == k){
i++;
int tmp = k-1;
while(tmp--){
num.pop();stab.pop();
}
}
else if(stab.top() == b[i]){
int tmp = num.top();
num.push(tmp+1);
stab.push(b[i]);i++;
}
}
}
if(staa.size() == stab.size()){
int flag = 0;
while(!staa.empty()){
if(staa.top() != stab.top()){
flag = 1;break;
}
staa.pop();stab.pop();
}
if(flag)puts("No");
else puts("Yes");
}
else puts("No");
}
return 0;
}
/*
1 1
0
1
4 3
001111
110000
001
110
*/