黯然销魂?
Time Limit: 2000/1000ms (Java/Others)
Problem Description:
发神集训表现出色,决定要送他鸡翅,但是锴神决定不让他那么容易拿到,所以给鸡翅包了很多盒子,盒子能装其他盒子。其中有个盒子,只装了一个黯然销魂BT辣鸡翅,现在发神要拿到黯然销魂鸡翅,问需要打开多少盒子。
Output:
对于每组数据,输出发神最少需要打开的盒子数目。
Sample Input:
((B)())
(B)
Sample Output:
2
1
解题思路:只需统计字符'B'的左边除去有空括号对后剩下左括号的个数即为最小需要打开的盒子数,水过!
AC代码:
1 #include<bits/stdc++.h>
2 using namespace std;
3 int main(){
4 char str[1005];int key,l;
5 while(cin>>str){l=0;
6 for(int i=0;str[i]!='\0';++i)
7 if(str[i]=='B'){key=i;break;}
8 for(int i=0;i<key;++i){
9 if(str[i]=='(')l++;
10 else l--;//匹配到一对空括号就不用打开,相应数量减1
11 }
12 cout<<l<<endl;
13 }
14 return 0;
15 }