CodeForces 556A Case of the Zeros and Ones

来源:互联网 发布:外国语大学网络教育 编辑:程序博客网 时间:2024/05/20 05:24

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 test(s)

input

4
1100

output

0

input

5
01010

output

1

input

8
11101111

output

6

题解找出输入0和1的序列中所有10或者01短序列,并且删除。得到最后序列的长度
//代码实现#include<iostream>#include<cstdio>#include<cstring>#define MAX 200010using namespace std;char str [MAX];int main(){    long long t;    long long cnout=0;    scanf("%lld",&t);    scanf("%s",str);    for(int i=0;i<t;i++)    {        if(str[i]=='0') //找出序列中的0字符            cnout++;//对0的个数进行计数    }    long long x,y,z;    x=cnout;//0的个数    y=t-cnout;//1的个数    if(x==y)//如果0和1的个数相等,所以的都会匹配得到0        z=0;    if(x<y)//如果不相等得到不想匹配的序列长度        z=t-2*x;    else        z=t-2*y;    printf("%lld",z);    return 0;}
0 0
原创粉丝点击