删除相邻的 01 直到无法继续删除,直接stl的list模拟一发就好了
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <list>
#include <vector>
using namespace std;
list <int> sb;
list <int> ::iterator it,nx;
int main()
{
int n;
scanf("%d",&n);
int tmp;
int i;
for (i=1;i<=n;i++)
{
scanf("%1d",&tmp);
sb.push_back(tmp);
}
int line=0;
for (it=sb.begin();it!=sb.end();)
{
nx=it;
nx++;
if (nx==sb.end()) break;
if ((*it==1&&*nx==0 )||(*it==0&&*nx==1))
{
it=sb.erase(it);
//printf("%d--\n",*it);
it=sb.erase(it);
// printf("%d--\n",*it);
// it--;
if (it!=sb.begin())
it--;
line=1;
}
if (!line)
it++;
line=0;
}
printf("%d\n",sb.size());
return 0;
}