堆栈练习2:HDOJ1870

来源:互联网 发布:mac 10.7.5怎么升级 编辑:程序博客网 时间:2024/06/01 08:14

首先是cin读取字符串。

之后按照括号匹配压栈弹栈就好了。

最后输出栈的大小。

#include <iostream>#include<stack>#include<string.h>#include<cstdio>using namespace std;char in[1005] ;int main(){    int len = 0 ;    stack<char> s ;    while(cin >> in)    {        len = strlen(in) ;        while( !s.empty() )            s.pop();        for(int i = 0 ; i < len ; i ++)        {            if(in[i]=='(')            {                s.push(in[i]);            }            else if(in[i]==')')            {                s.pop();            }            else            {                cout << s.size() << endl ;                break ;            }        }    }    return 0;}


0 0