程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 第五次比賽的水題,第五次比賽水題

第五次比賽的水題,第五次比賽水題

編輯:C++入門知識

第五次比賽的水題,第五次比賽水題


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 }

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved