A. Keanu Reeves

传送门

A. Keanu Reeves

time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

After playing Neo in the legendary “Matrix” trilogy, Keanu Reeves started doubting himself: maybe we really live in virtual reality? To find if this is true, he needs to solve the following problem.
Let’s call a string consisting of only zeroes and ones good if it contains different numbers of zeroes and ones. For example, 1, 101, 0000 are good, while 01, 1001, and 111000 are not good.
We are given a string s of length n consisting of only zeroes and ones. We need to cut s into minimal possible number of substrings s1,s2,…,sk such that all of them are good. More formally, we have to find minimal by number of strings sequence of good strings s1,s2,…,sk such that their concatenation (joining) equals s, i.e. s1+s2+⋯+sk=s.
For example, cuttings 110010 into 110 and 010 or into 11 and 0010 are valid, as 110, 010, 11, 0010 are all good, and we can’t cut 110010 to the smaller number of substrings as 110010 isn’t good itself. At the same time, cutting of 110010 into 1100 and 10 isn’t valid as both strings aren’t good. Also, cutting of 110010 into 1, 1, 0010 isn’t valid, as it isn’t minimal, even though all 3 strings are good.
Can you help Keanu? We can show that the solution always exists. If there are multiple optimal answers, print any.

Input
The first line of the input contains a single integer n (1≤n≤100) — the length of the string s.
The second line contains the string s of length n consisting only from zeros and ones.

Output
In the first line, output a single integer k (1≤k) — a minimal number of strings you have cut s into.
In the second line, output k strings s1,s2,…,sk separated with spaces. The length of each string has to be positive. Their concatenation has to be equal to s and all of them have to be good.
If there are multiple answers, print any.

Examples
input
1
1
output
1
1

input
2
10
output
2
1 0

input
6
100011
output
2
100 011

Note
In the first example, the string 1 wasn’t cut at all. As it is good, the condition is satisfied.
In the second example, 1 and 0 both are good. As 10 isn’t good, the answer is indeed minimal.
In the third example, 100 and 011 both are good. As 100011 isn’t good, the answer is indeed minimal.

题意:给一串只包含0和1的字符串,将字符串分成多个字串,并且字串相加可以得到原来的字符串,问如何分最小个数的字串使得每个字串中包含的1和0的个数不相等。

思路:对于输入的字符串,如果本来就是0和1的个数是不相等,那么就是本身,直接输出即可。如果是相等的,因为可以任意输出,那么就将第一个字符当做一个字串,后面的为一个字串,那么两个字串的0和1的个数肯定是不相等的。

代码

#include<bits/stdc++.h>
using namespace std;
char a[105];
int n;
int main()
{
	while (cin >> n) {
		cin >> a;
		int sum0 = 0, sum1 = 0;
		for (int i = 0; i < n; i++) {
			if (a[i] == '1')
				sum1++;
			else
				sum0++;
		}
		if (sum0!= sum1) {
			printf("1\n%s\n", a);
		}
		else{
		printf("2\n");
		printf("%c ", a[0]);
		for (int i = 1; i < n; i++)
			printf("%c", a[i]);
		printf("\n");
	}
	}return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

稚皓君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值