LeetCode 856. Score of Parentheses brackets score

This question is actually a variant (Hong Kong with a classmate of C / C ++ job)

Add a little more difficult, input S is not necessarily judge for themselves whether the balance is the balance, the need, if unbalanced output to zero.

Title Description

Given a parentheses string s, compute the score of the string based on the following rule:
• If s is not balanced, the score is 0.
• () has score 1.
• AB has score A + B, where A and B are balanced parentheses strings.
• (A) has score 2 * A, where A is a balanced parentheses string.
A balanced string satisfies the following requirements:
• The number of ‘(’ equals the number of ‘)’ in the string.
• Counting from the first position to any position in the string, the number of ‘(’ is
greater than or equal to the number of ‘)’ in the string. (For example, ‘)(’ is not
balanced.)
The length of s is at most 30.
Input:
The input is a parentheses string s.
Output:
Print the score of s.
Examples:
1. Input:
(())
Output:
2
2. Input:
(()(()))
Output:
6
3. Input:
((()())
Output:
0

A simple method

#include<iostream>
#include<string>
using namespace std;
    int scoreOfParentheses(string S) {
        int lef=0,flag=0;

        int cnt = 0;
        int res = 0;
        char last = ' ';
        char ch;
        for (int i=0;i<S.length();i++) {
            ch=S[i];
            if(CH == ' ( ' ) { 
                CNT ++ ; 
                LEF ++ ; 
            } 
            the else {
                 // depth becomes smaller 
                cnt-- ; 
                LEF - ;
                 // a is' (table name pair is (), a plus 
                IF (Last == ' ( ' ) { 
                    RES + = . 1 << CNT; 
                } 
            } 
            // record character 
            IF (LEF < 0  )
            {
                flag=1;
                break;
            }
            last = ch;
        }
        if(flag||(lef>0))
        res=0;
        return res;
    }
int main()
{
    string s;
    cin>>s;
    int ans=scoreOfParentheses(s);
    cout<<ans;
    system("pause");
    return 0;
}

Utilizing the method specified in the form of a stack of good

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


struct node {
    int sc;
    char type;
    node(int x): sc(x) ,type('p') {}
};
stack<node> A;
void SolveC() {
    string s;
    cin >> s;
    int n = s.length();
    int score = 0;
    int temp = 0;
    int temp1 = 0;
    node* nnode;
    for (int i = 0; i <= (n - 1); i++) {
        if (s[i] == '(') {
            nnode = new node(0);
            nnode->type = '(';
            A.push(*nnode);

        }
        if (s[i] == ')')
        {
            if (A.empty()) {
                cout << 0;
                return;
            }
            if ( A.top().type == '(') {
                A.pop();
                nnode = new node(0);
                nnode->sc = 1;
                nnode->type='p';//add type
            //    if (A.top().type == '(' || A.empty()) {
                /*if (A.empty()||A.top().type == '('  ) {
                    A.push(*nnode);
                }*/
                A.push(*nnode);
                temp=0;  //ADD
                if (A.top().type == 'p') {
                    while (!A.empty() && (A.top().type == 'p') ) {
                        temp1 = A.top().sc;
                        A.pop();
                        temp = temp + temp1;
                    }
                    nnode = new node(0);
                    nnode->sc = temp;
                    nnode->type='p';
                    A.push(*nnode);
                }
                continue; //ADD
            }
            if ( A.top().type == 'p'){
                temp = A.top().sc;
                A.pop();
                if(A.empty()==1)   //
                {
                    cout<<0;
                    return;
                }
                if (A.top().type == '(') {
                    A.pop();
                    temp = temp * 2;
                    while (!A.empty() && (A.top().type == 'p')) {
                        temp1 = A.top().sc;
                        A.pop();
                        temp = temp + temp1;
                    }
                    

                    nnode = new node(0);
                    NNode -> SC = TEMP; 
                     NNode -> type = ' P ' ; 
                    A.push ( * NNode); 
                } 

            } 
                    the else { 
                    COUT << 0 ;
                     return ; 
                } 
        } 
    } 
    
    Score = A.top () SC;. 

     // check if ((excessive 
    A.pop (); 
   
    iF (A.empty ());
     the else 
    { 
        Score = 0 ;
    }
    cout << score;

    return;

}

int main() {
    SolveC();
        system("pause");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lqerio/p/11708619.html