Water issues in the fifth and fifth Competitions

Source: Internet
Author: User

Water issues in the fifth and fifth Competitions

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 lengthNConsisting 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 lengthNAccept-limit 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 integerN(1 digit ≤ DigitNLimit ≤ limit 2 · 105), the length of the string that Andreid has.

The second line contains the string of lengthNConsisting 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 to 10 and then empty

In the second sample test it is possible to change the string like the following:. 01010 to 010 to 0

In the third sample test it is possible to change the string like the following:. 11101111 to 111111

 

 

Give a string (note that it is a string). If the two adjacent strings are 0 and 1, take them and output the length of the string, it is worth noting that only the output and input of a group of data are required .......

 

Solution: When I first saw the question and understood what it meant, I was almost overwhelmed by it. The first thought is to mark recursion or something, and then think about it carefully. In fact, this is not necessary. As long as there are 0 and 1 in the string, it will definitely play out. In this way, we can convert the problem to see how many strings are 1 and 0, and subtract less with more strings. The rest is the string length .......

 

(Note that the string is used here. The first response to the number is to use an array, and no output is made. I knew it was actually stuck in the array for more than half an hour. It's cool ....

Finally, a string is used, because the question indicates that a string is used, and arrays cannot be used to continuously enter numbers, for example, 1100, the program will regard 1100 as a number .......

I hope someone will make the same silly mistake as me ............ (I am so stupid to cry myself .....))

 

 

 

The Code is as follows:

 

 

 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     else20         printf("%d\n",m2-m1);21 22     return 0;23 }

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.