(Hdu step 8.1.7) Gifts for yu'e (basic use of stacks-evaluate the number of '(' before a specific character), hdu8.1.7
Question:
A gift for the Fool's Day |
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission (s): 49 Accepted Submission (s): 44 |
|
Problem Description April 1 is approaching. Vayko thought of a good way to give gifts to fools. Hey, don't think too well. This gift is not that simple. Vayko has prepared a bunch of boxes for the sake of fools, and a box contains a gift. There can be zero or multiple boxes in the box. Suppose there are no other boxes in the box where the gift is given.
Use () to represent a box, B to represent a gift, and Vayko wants you to help her calculate the fool's index, that is, the minimum number of boxes to be split to get a gift. |
Input this question contains multiple groups of tests, please process until the end of the file. Each group of tests contains a string of no more than 1000 characters including '(', ')' and 'B', representing the gift perspective designed by Vayko. You can assume that each perspective image is valid. |
Output for each group of tests, please Output the fool's index in one row. |
Sample Input((((B)()))())(B) |
Sample Output41 |
AuthorKiki |
Source2008 select-out competition-warm-up competition |
Recommendlcy |
Question Analysis:
Simple simulation.
The Code is as follows:
/** G. cpp ** Created on: March 24, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <stack> using namespace std; int main () {string str; while (getline (cin, str) {stack <char> st; int len = str. length (); int I; for (I = 0; I <len; ++ I) {// traverse the entire string if (str [I] = '(') {// if the current character is (st. push ('); // import the current character to the stack} else if (str [I] =') {// if the current character is) st. pop (); // an out-of-stack (} else if (str [I] = 'B') {// if the gift break has been found; // skip loop} int cnt = 0; while (st. empty () = false) {// how many (cnt ++; st. pop () ;}printf ("% d \ n", cnt) ;}return 0 ;}