Eva and Euro coins(Gym-101981E)

Problem Description

Eva is fond of collecting coins. Whenever she visits a different country, she always picks up as many local coins as she can. As you know, Eva also likes to go trips to Europe; thus she has collected a large amount of Euro coins because so many countries in Europe use them. 

Eva has n Euro coins in total. She places all her coins on a desk in a row and plays a game with the coins.In one step Eva can choose exactly k consecutive coins and flips them at the same time, provided that all heads of these coins face up or all heads of these coins face down. She wonders that, in finite steps, what states of the coins can be reached from the original state.

Input

The first line contains two integers, n and k (1 ≤ k ≤ n ≤ 106) — the number of Euro coins Eva owns and the number of consecutive coins Eva can flip in one step. The next two lines contain two strings, s and t, respectively (|s| = |t| = n). s and t only contain the digits 0 and 1. 

s represents the initial state of the n coins: if the head of the i-th coin faces up, then the i-th character of s is 1; otherwise (i.e. the head of i-th coin faces down), the i-th character of s is 0. t represents the desired final state of the n coins in the same way as s.

Output

If it is possible for Eva to reach the state represented by t from the state represented by s in finite steps, output "Yes"; otherwise, output "No" (without the quotes).

Sample Input

sample input 1

6 2
000000
101101

sample input 2

8 3
10101010
01010101

Sample Output

sample output 1

Yes

sample output 2

No

题意:给出两个长度为 n 的字符串 s、t,可以连续翻转 k 个相同的字符,问能否从 s 串变为 t 串

思路:由于每次只能改变连续个相同的状态,因此只需要在两个串中将连续 k 个相同的字符删去,把剩下的合成一个串,如果最后两个串相同,则说明可以达到,如果不同,则说明无法达到

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 400+5;
const int dx[] = {1,-1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

string getRes(string str,int k){
    stack<char> st;
    stack<int> num;
    for(int i=0;i<str.size();i++){
        if(st.empty()||st.top()!=str[i]){
            st.push(str[i]);
            num.push(1);
        }
        else{
            if(num.top()+1==k){
                st.pop();
                num.pop();
            }
            else{
                int temp=num.top();
                num.pop();
                num.push(temp+1);
            }
        }
    }

    string res="";
    while(!st.empty()){
        char temp=st.top();
        st.pop();
        int cnt=num.top();
        num.pop();
        for(int i=1;i<=cnt;i++)
            res+=temp;
    }
    return res;
}
int main() {
    int n,k;
    scanf("%d%d",&n,&k);
    string str1,str2;
    cin>>str1>>str2;

    if(k==1)
        printf("Yes\n");
    else{
        if(getRes(str1,k)==getRes(str2,k))
            printf("Yes\n");
        else
            printf("No\n");
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值