hdu3351 stack

大括号配对

输入的时候排除掉已经配对好的,剩余的情况为以下三种:

...}}}....

....{{{...

....}}}...{{{...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>

const int inf = (1<<31)-1;
const int MAXN = 1e5+10;

using namespace std;

stack<char>a;
int main()
{
    string s1;
    int mc=1;
    while(cin>>s1&&s1[0]!='-'){
        int n = s1.length();
        for(int i=0;i<n;i++){
            if(a.empty()){
                a.push(s1[i]);
            }else{
                if(a.top()=='{'&&s1[i]=='}')
                    a.pop();
                else
                    a.push(s1[i]);
            }
        }
        int t1,t2;
        t1 = t2 = 0;
        while(!a.empty()){
            if(a.top()=='{')
                t1++;
            else
                t2++;
            a.pop();
        }
        cout<<mc++<<". ";
        cout<<(t1+1)/2+(t2+1)/2<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}
View Code

 

posted @ 2016-04-14 21:05  iEdson  阅读(148)  评论(0编辑  收藏  举报