A. Case of the Zeros and Ones----解题报告
A. Case of the Zeros and Ones
Description
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Sample Input
4 1100
0
5 01010
1
8 11101111
6
Hint
In the first sample test it is possible to change the string like the following: .
In the second sample test it is possible to change the string like the following: .
In the third sample test it is possible to change the string like the following: .
题目大意:
有一个只由0和1组成的字符串,每次可以任意选择相邻的两个字符且这两个字符一个为是0,一个是1(顺序无所谓)并去掉这两个位置得到一个新的字符串,然后可以对新串继续操作,直到不能继续操作为止。求这个最短的长度为多少。
分析:
观察输入输出后,发现其实就是计算0,1的个数,然后用大的减小的,输出不能配对的总个数。
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<iostream>
3 using namespace std;
4
5 const int maxn=200001;
6
7 int main()
8 {
9 int n,i;
10 scanf("%d",&n);
11 char a[maxn];
12 scanf("%s",a);
13 int m0=0,m1=0;
14 for( i=0;i<=n;i++)
15 {
16 if(a[i]=='0')
17 m0++;
18 if(a[i]=='1')
19 m1++;
20 }
21 int b=m1-m0;
22 int c=m0-m1;
23 if(m0<=m1)
24 printf("%d\n",b);
25 else
26 printf("%d\n",c);
27 return 0;
28 }
由于我输入的是字符型,判断0,1时要带引号,这要注意。