HDU 5479

题意:问 给出的一串括号,有几对是匹配括号。

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int main(){
    string s;
    int cnt[1001]={0},n;
    scanf("%d",&n);
    for(int j=1;j<=n;j++)
    {
        stack<char>st;
        cin>>s;
        for(int i=0;i<s.length();++i){
            if(!st.empty())
            {
                char t = st.top();
                if(t=='('&&s[i]==')')
                {
                    st.pop();
                    cnt[j]++;
                }
                else
                {
                    st.push(s[i]);
                }
            }
            else
            {
                st.push(s[i]);
            }
        }
    }
    for(int i = 1;i<=n;i++) 
    printf("%d\n",cnt[i]); 
    return 0;
}

 

posted @ 2019-02-22 11:06  鹤花之歌  阅读(85)  评论(0编辑  收藏  举报