码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3351 stack

时间:2016-04-14 22:20:25      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

大括号配对

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

...}}}....

....{{{...

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

技术分享
#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

 

hdu3351 stack

标签:

原文地址:http://www.cnblogs.com/EdsonLin/p/5392733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!