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 adjacentpositions 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
Input
4 1100
Output
0
Input
5 01010
Output
1
Input
8 11101111
Output
6
Hint
In the first sample test it is possible to change the string like the following: .1100到10再到空
In the second sample test it is possible to change the string like the following: .01010到010到0
In the third sample test it is possible to change the string like the following: .11101111到111111
题意:给一个字符串(注意是字符串),如果相邻的两个是0和1的话,就把他们拿去,一直这样最后输出字符串的长度,还有值得注意的就是这里只要求输出和输入一组数据......。
解题思路:开始看见题目,理解玩意思后,差点就被它唬住了。第一想到的是标记递归什么的,然后仔细一想,其实不用这么麻烦。字符串中只要有0和1,就一定会弹出去。这样就可以把问题转化为看这个字符串有多少个1和0,用多的减去少的,剩下的就是字符串长度.......
(注意这里要用字符串,我看见数字第一反应就是用数组,然后就一直没有输出。明明知道做却卡在了数组上半个小时多,这酸爽....
最后还是用了字符串,因为题目意思是用字符串,并且用数组不可以连续输入数字,例如输入1100,程序会将1100理解为一个数字.......
希望有人和我犯了一样的傻逼错误的时候可以改过来............(真是自己把自己蠢哭.....))
代码如下:
1 #include <stdio.h> 2 #include <string> 3 char a[200010]; 4 int main() 5 { 6 int n; 7 scanf("%d",&n); 8 int m1=0,m2=0; 9 scanf("%s",&a); 10 for(int j=0; j<n; j++) 11 { 12 if(a[j]==‘0‘) 13 m1=m1+1; 14 if(a[j]==‘1‘) 15 m2=m2+1; 16 } 17 if(m1>=m2) 18 printf("%d\n",m1-m2); 19 else 20 printf("%d\n",m2-m1); 21 22 return 0; 23 }