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

http://codeforces.com/problemset/problem/556/A

题意:
输入一个只包含0和1的字符串,如果1和0相邻,则这2个数字消去。
问最后剩下的字符串长度是多少。

PS:
统计0和1的个数就可以,因为无论1和0在什么位置,只要出现不同一定能消去,而与所处位置无关。直到只剩一种数字。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)

int n;
string s;

int main() {
cin >> n;
cin >> s;
int n0 = 0, n1 = 0;
FOR(i, 0, n) {
if (s[i] == '0') n0++;
else n1++;
}
printf("%d\n", n - min(n0, n1) * 2);
}