april fool's gift

Topic description:
April 1st is approaching, Vayko thought of a fool's good way - to send gifts. Hey, don't think too much, this gift is not that simple, Vayko prepared a bunch of boxes for the fool, and one of the boxes contained a gift. There can be zero or more boxes inside the box. Suppose there are no other boxes in the box where the gift is placed.

Use ( ) to represent a box, and B to represent a gift. Vayko wants you to help her calculate the Fool’s Index, that is, how many boxes need to be opened at least to get the gift.

Input:
This question contains multiple sets of tests, please process until the end of the file. Each set of tests consists of a string of length not greater than 1000, containing only three characters of '(', ')' and 'B', representing a gift perspective designed by Vayko. You can assume that every perspective drawing is legal.

Output:
For each set of tests, output the fool's index on one line.

Sample Input
((((B)()))())
(B)
Sample Output
4
1

Method 1:
Ideas: Although this problem can be clearly seen to be solved with the idea of ​​stacks, there is actually a simpler method. According to the meaning of the question, there are only three kinds of characters () B, so to know the number of boxes, just know how many characters are in front of B (characters, but there may be (() B) in this case, there is in front of B), but because It is legal, so it can be found (and) before B, and it is eliminated, so the number of boxes is the number of (subtracted the number of) in the string before B.

#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
    char str[1005];
    short i,numL,numR;
    while(scanf("%s",str)!=EOF)
    {
        numL=numR=i=0;
        while(str[i]!='B')
        {
            if(str[i]=='(') numL++;
            else numR++;
            i++;
        }
        cout << numL-numR << endl;
    }
}

Ideas:
1. When encountering a left bracket, push the stack, num++;
2. When encountering B, no matter
3. When encountering a closing bracket, if there is a left bracket at the top of the stack, pop the left bracket from the stack, num– ;
4. The left brackets of those empty boxes will be popped off the stack, and the rest will be the left brackets of the boxes with gifts

#include<iostream>
#include<string>
#include<stack>
using namespace std;

int main()
{
    char a[1222];
    while(scanf("%s",a)!=EOF)
    {
        int lena = strlen(a);
        stack<char> s;
        while(!s.empty())//将栈清空
            s.pop();

        int num = 0;
        for(int i = 0;i < lena;i++)
        {
            if(a[i] == '(')
            {
                s.push(a[i]);
                num++;
            }
            else if(a[i] == ')'&&!s.empty()&&s.top() == '(')
            {
                s.pop();
                num--;
            }
            else break;
        }
        if(!s.empty())
            cout << num << endl;
        else cout << "0" << endl;
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325806345&siteId=291194637