四月一日快到了,Vayko想了个愚人的好办法——送礼物。嘿嘿,不要想的太好,这礼物可没那么简单,Vayko为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物。盒子里面可以再放零个或者多个盒子。假设放礼物的盒子里不再放其他盒子。
用()表示一个盒子,B表示礼物,Vayko想让你帮她算出愚人指数,即最少需要拆多少个盒子才能拿到礼物。
Input
本题目包含多组测试,请处理到文件结束。
每组测试包含一个长度不大于1000,只包含'(',')'和'B'三种字符的字符串,代表Vayko设计的礼物透视图。
你可以假设,每个透视图画的都是合法的。
Output
对于每组测试,请在一行里面输出愚人指数。
Sample Input
((((B)()))()) (B)
Sample Output
4 1
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <string.h>
using namespace std;
char a[200005];
stack<char>s;
int main()
{
while(scanf("%s",a)!=EOF)
{
int len = strlen(a);
int sum = 0;
while(!s.empty())
{
s.pop();
}
for(int i=0;i<len;i++)
{
if(s.empty())
{
sum++;
s.push(a[i]);
}
else
{
if(i!=0)
{
if((s.top()=='('&&a[i]=='(')||(s.top()==')'&&a[i]=='(')||(s.top()==')'&&a[i]==')')||(a[i]=='B')||(s.top()=='B'&&a[i]=='(')||(s.top()=='B'&&a[i]==')'))
{
sum++;
s.push(a[i]);
}
else if(s.top()=='('&&a[i]==')')
{
s.pop();
s.push(a[i]);
s.pop();
sum--;
// i++;
}
}
else
{
sum++;
s.push(a[i]);
}
}
}
// printf("sum = %d\n",sum);
printf("%d\n",(sum-1)/2);
}
while(!s.empty())
{
s.pop();
}
return 0;
}