Problem Description April 1 is coming, Vayko think of a fool's good way-to give gifts. Hey, don't think too good, this gift is not so simple, vayko for a fool, prepared a pile of boxes, one of which contained a gift. You can put 0 or more boxes inside the box. Suppose the gift box is no longer placed in another box.
Use () to represent a box, b for a gift, Vayko want you to help her figure out the Fool's index, that is, the minimum number of boxes to be removed to get the gift.
Input This topic contains multiple sets of tests, please handle to the end of the file.
Each set of tests contains a string that is not more than 1000 in length, contains only ' (', ') ' and ' B ' three characters, representing the Vayko design of the gift perspective.
You can assume that each perspective picture is legal.
Output for each set of tests, please export the fool's index in one line.
Sample Input
B
Sample Output
4 1
This problem can actually be used in a very water way.
But since you have to exercise yourself,
Of course, do it with stacks.
#include <cstdio>
#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
int main ()
{
stack<char> S;
int sum,len,i;
Char str[10000];
while (the gets (str))
{while
(! S.empty ())
s.pop ();
len = strlen (str);
sum = i = 0;
while (I<len)
{
if (str[i] = = ' (')
{
s.push (str[i]);
i++,sum++;
}
else
{
if (str[i] = = ') ')
{
s.pop ();
Sum--, i++;
}
else break
;
}
}
if (! S.empty ())
printf ("%d\n", sum);
}
return 0;
}