A. Case of the Zeros and Ones(Codeforces Round #310 (Div. 2) 栈)

来源:互联网 发布:淘宝达人赚钱 编辑:程序博客网 时间:2024/06/05 03:28
A. Case of the Zeros and Ones
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
41100
output
0
input
501010
output
1
input
811101111
output
6
Note

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: .



   题意:有一个n个数字的序列,如果相邻的两个数字是10或者01就可以消除,消

除之后由原来不相邻变为相邻的两个数字符合这种情况同样可以消除,问消除之后这

个序列还剩几个数字

   思路:栈正好就好符合这种特性;



#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<stack>using namespace std;int n;char str[200010];stack<char>q;int main() {    while(scanf("%d",&n)!=EOF) {        scanf("%s",str);        while(!q.empty()) {            q.pop();        }        for(int i=0; str[i]!='\0'; i++) {            if(q.empty()) {                q.push(str[i]);            } else {                if((q.top() == '1' && str[i] == '0') || (q.top() == '0' && str[i] == '1')) {                    q.pop();                } else {                    q.push(str[i]);                }            }        }        printf("%d\n",q.size());    }    return 0;}


0 0