coderforce A. Case of the Zeros and Ones

题意:相邻的0和1或者1和0可以消除,问最后字符串还有多少个字符
#include<stdio.h>
#include<string.h>
#include<string>
#include<stack>
#include<algorithm>
using namespace std;
const int maxm=1e5+10;
char s[maxm<<1];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%s",s);
        stack<char>q;
        int temp=n;
        for(int i=0; i<n; i++)
        {
            if(!q.empty())
            {
                char a=q.top();
                if(s[i]!=a)
                {
                    temp-=2;
                    q.pop();
                }
                else
                {
                    q.push(s[i]);
                }
            }
            else
            {
                q.push(s[i]);
            }
        }
        printf("%d\n",temp);
    }
    return 0;
}

你可能感兴趣的:(coderforce A. Case of the Zeros and Ones)