2018 ACM-ICPC 南京站 E Eva and Euro coins (思维)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("Yes\\n");
else
printf("No\\n");
return 0;
以上是关于2018 ACM-ICPC 南京站 E Eva and Euro coins (思维)的主要内容,如果未能解决你的问题,请参考以下文章
ACM-ICPC 2018 南京赛区网络预赛 E AC Challenge 状压DP
[计蒜客] ACM-ICPC 2018 南京赛区网络预赛 | 部分题解 | 线段树 + 线性筛 + 最短路