题意:问 给出的一串括号,有几对是匹配括号。
#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 ",cnt[i]); return 0; }