http://acm.hdu.edu.cn/showproblem.php?pid=1870
栈模拟:15MS
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<stack> 3 #include<cstring> 4 using namespace std ; 5 char a[1005] ; 6 stack<char>s ; 7 int main() 8 { 9 while(cin>>a) 10 { 11 while(!s.empty()) 12 s.pop() ; 13 int len = strlen(a) ; 14 for(int i=0; i<len; i++) 15 { 16 if(a[i]=='B') 17 { 18 cout<<s.size()<<endl ; 19 break ; 20 } 21 if(a[i]=='(') 22 s.push(a[i]) ; 23 if(a[i]==')') 24 s.pop() ; 25 } 26 } 27 return 0 ; 28 }
数组模拟:0MS
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstring> 3 using namespace std ; 4 int main() 5 { 6 char a[1005] ; 7 while(cin>>a) 8 { 9 int count = 0 ; 10 int len = strlen(a) ; 11 for(int i=0; i<len; i++) 12 { 13 if(a[i]=='(') 14 count++ ; 15 else 16 if(a[i]==')') 17 count-- ; 18 else 19 break ; 20 } 21 cout<<count<<endl ; 22 } 23 return 0 ; 24 }