coderforce A. Case of the Zeros and Ones

来源:互联网 发布:mac中如何复制粘贴 编辑:程序博客网 时间:2024/06/05 00:13
题意:相邻的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;}

0 0
原创粉丝点击