栈模拟
一个一个读入数字,如果栈顶元素和该字符不同,则弹出栈顶元素
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n;
int a[N], top;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string str;
cin >> n >> str;
for (int i = 0; i < n; i++)
{
int x;
x = str[i] - '0';
if (top && 1 - a[top - 1] == x)
top--;
else
a[top++] = x;
}
cout << top << endl;
return 0;
}