我是靠谱客的博主 傲娇大神,最近开发中收集的这篇文章主要介绍2018 ACM-ICPC 南京站 E Eva and Euro coins (思维),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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 kk 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, 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.tt represents 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

题目来源

ACM-ICPC Nanjing Onsite 2018

 

题意:

给两串n个01串,你可以翻转连续的k个相同的数,问第一个状态是否能达到第二个状态?

分析:

因为我们只能改变相同连续的k个状态(一种假设k=2),00->11,11->00,同时还有1001->1111,1001也可以到0000,所以两个字符串如何在满足题意规则某部分串一定可以变为全0或者全1的状态,即把相邻相同的k个状态合并起来,注意1001可以合并成空。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,m;
stack<char>st;
stack<int>num;
string cal(string s)
{
	for(int i=0;i<s.size();i++)
	{
		if(!st.empty()&&st.top()==s[i])
		{
			//cout<<s[i]<<" "<<num.top()<<" "<<m<<endl;
			if(num.top()+1==m)
			{
				st.pop();
				num.pop();
			}
			else
			{
				int temp=num.top();
				num.pop();
				num.push(temp+1);
			}
		}
		else
		{
			st.push(s[i]);
			num.push(1);
		}
	}
	string res="";
	while(!st.empty())
	{
		char temp=st.top();
		int num1=num.top();
		for(int i=1;i<=num1;i++)
		{
			res+=temp;
		}
		st.pop();
		num.pop();
	}
	//cout<<res<<endl;
	return res;
}
string s1,s2;
int main()
{
    
    scanf("%d%d",&n,&m) ;
    cin>>s1>>s2;
    if(m==1||cal(s1)==cal(s2))
	{
	    printf("Yesn");	
	}
	else
		printf("Non");

    return 0;
}

 

最后

以上就是傲娇大神为你收集整理的2018 ACM-ICPC 南京站 E Eva and Euro coins (思维)的全部内容,希望文章能够帮你解决2018 ACM-ICPC 南京站 E Eva and Euro coins (思维)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部