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 nn 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 kkconsecutive 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, nn and kk (1 \le k \le n \le 10^61≤k≤n≤106) \text{---}—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, ss and tt, respectively (|s| = |t| = n∣s∣=∣t∣=n). ss and tt only contain the digits 00 and 11.
ss represents the initial state of the nn coins: if the head of the ii-th coin faces up, then the ii-th character of ss is 11; otherwise (i.e. the head of ii-th coin faces down), the ii-th character of ss is 00.ttrepresents the desired final state of the nn coins in the same way as ss.
Output
If it is possible for Eva to reach the state represented by tt from the state represented by ss in finite steps, output "Yes"; otherwise, output "No" (without the quotes).
样例输入1复制
6 2 000000 101101
样例输出1复制
Yes
样例输入2复制
8 3 10101010 01010101
样例输出2复制
No
题目来源
正确的做法是连续k个相同的就消去!,开始想的是从左到右枚举,最后发现其实再往深考虑就是连续k个消去,我太菜了。
模拟消去的过程就是用一个栈,不断判断。
若当前栈>=t-1个数,那么判断当前的数和栈内后t-1个数是否相同,相同则消去,否则不消。因为判断栈内多少个数和当前一样时,用了pop,而保证pop的值是和当前值一样的,直接再Push进去即可!!!
#include <algorithm> //STL通用算法
#include <bitset> //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex> //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque> //STL双端队列容器
#include <exception> //异常处理类
#include <fstream>
#include <functional> //STL定义运算函数(代替运算符)
#include <limits>
#include <list> //STL线性列表容器
#include <map> //STL 映射容器
#include <iomanip>
#include <ios> //基本输入/输出支持
#include<iosfwd> //输入/输出系统使用的前置声明
#include <iostream>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <queue> //STL队列容器
#include <set> //STL 集合容器
#include <sstream> //基于字符串的流
#include <stack> //STL堆栈容器
#include <stdexcept> //标准异常类
#include <streambuf> //底层输入/输出支持
#include <string> //字符串类
#include <utility> //STL通用模板类
#include <vector> //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 1000000+66;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
int n,m;
char ch[maxn];
char ch1[maxn];
stack<char>s;
stack<char>s1;
queue<char>tmp;
int vis[maxn];
int vis1[maxn];
int main()
{
int n,t;
scanf("%d %d",&n,&t);
scanf("%s %s",ch+1,ch1+1);
int t1=0,t2=0;
int flag=0;
if(t==1)
{
printf("Yes\n");
return 0;
}
for(int i=1; i<=n; i++)
{
if(ch[i]=='0')
{
if(s.size()>=t-1)
{
int l1=0;
while(s.size()&&s.top()=='0')
{
l1++;
s.pop();
}
if(l1==t-1)
{
continue;
}
else
{
for(int o=1;o<=l1;o++)
{
s.push(ch[i]);
}
}
}
}
else
{
if(s.size()>=t-1)
{
int l1=0;
while(s.size()&&s.top()=='1')
{
l1++;
s.pop();
}
if(l1==t-1)
{
continue;
}
else
{
for(int o=1;o<=l1;o++)
{
s.push(ch[i]);
}
}
}
}
s.push(ch[i]);
vis[i]=s.size();
}
for(int i=1; i<=n; i++)
{
if(ch1[i]=='0')
{
if(s1.size()>=t-1)
{
int l1=0;
while(s1.size()&&s1.top()=='0')
{
tmp.push(s1.top());
l1++;
s1.pop();
}
if(l1==t-1)
{
continue;
}
else
{
for(int o=1;o<=l1;o++)
{
s1.push(ch1[i]);
}
}
}
}
else
{
if(s1.size()>=t-1)
{
int l1=0;
while(s1.size()&&s1.top()=='1')
{
tmp.push(s1.top());
l1++;
s1.pop();
}
if(l1==t-1)
{
continue;
}
else
{
for(int o=1;o<=l1;o++)
{
s1.push(ch1[i]);
}
}
}
}
s1.push(ch1[i]);
}
if(s.size()!=s1.size())
{
flag=1;
}
else
{
while(!s.empty())
{
if(s1.top()!=s.top())
{
flag=1;
break;
}
s1.pop();
s.pop();
}
}
if(flag)printf("No\n");
else printf("Yes\n");
}