# Swust 12th acm 邀请赛# [ E ] 01 String [题解]
简明题意
一个长为n的字符串由0 、 1组成.
操作:如果2个相邻字符中一个1一个0就可以删除这2个字符得到n-2的字符串
求:任意次操作后最短长度。
Input
T组数据(<=40)
每组数组2行
1: n 长度
2: 字符串
样例
3
4
1100
5
01010
8
11101111
样例答案
0
1
6
思路:贪心。水题之一,答案就是0和1的个数之差
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 10005
int main() {
int T;
char str[N];
scanf("%d", &T);
while(T--) {
int one = 0;
int n = 0;
scanf("%d", &n);
scanf("%s", str);
for(int i = 0; i < n; ++i) {
one += (str[i] == '1');
}
printf("%d\n", abs(n - one - one));
}
return 0;
}