zoukankan      html  css  js  c++  java
  • 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
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    C# 使用布尔操作符
    C# 复合赋值操作符
    C# while语句
    C# do while语句
    datatabe 与string
    打开外部程序并
    group by 显示
    GROUP by 方法  C#
    屏幕取色
    C#简单继承示例详解——快速入门
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5392733.html
Copyright © 2011-2022 走看看