CF1504C. Balance the Bits

时间:2021-07-12
本文章向大家介绍CF1504C. Balance the Bits,主要包括CF1504C. Balance the Bits使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Archie

一道比较显然的构造题

显然1和0的数量都需要是偶数,不然必挂

显然开头和结尾必须相互匹配,不然也挂

然后用1把这里分成一小块一小块,每一块,如果有偶数个零,显然可行

奇数个零的块必然成双存在,同上处理

01分开构造

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int t;
int n;
string s,s1,s2,s3;

int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		cin>>s;
		int l=n;
		s1=s3;
		s2=s3;
		int c1=0,c2=0;
		for(int i=1;i<l-1;++i){
			if(s[i]=='0'){
				c1++;
				if(c1%2==1){
					s1+='(';
					s2+=')';
				}else{
					s1+=')';
					s2+='(';
				}
			}else{
				c2++;
				if(c2%2==1){
					s1+='(';
					s2+='(';
				}else{
					s1+=')';
					s2+=')';
				}
			}
		}
		if(s[0]=='0'||s[l-1]=='0'||(c1&1)||(c2&1)){
			printf("NO\n");
		}else{
			printf("YES\n");
			cout<<"("<<s1<<")"<<"\n"<<"("<<s2<<")"<<endl;
		}
	}
	return 0;
}

原文地址:https://www.cnblogs.com/For-Miku/p/15001816.html