HDU1870: The Gift of April Fool's Day

Source: Internet
Author: User
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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.