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
101101sample input 2
8 3
10101010
01010101Sample 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;
}