zoukankan      html  css  js  c++  java
  • LeetCode 856. Score of Parentheses 括号的分数

    其实是这道题的变式(某港带同学的C/C++作业)

    增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0.

    题目描述

    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

    一种简单的方法

    #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++;
                }
                else {
                    // 深度变小
                    cnt--;
                    lef--;
                    // 上一个是'('表名这一对是(),可以加分
                    if (last == '(') {
                        res += 1 << cnt;
                    }
                }
                // 记录字符
                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;
    }

    一种利用了规定好形式的栈的方法

    #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);
                    }
    
                }
                        else {
                        cout << 0;
                        return;
                    }
            }
        }
        
        score = A.top().sc;
    
         //检查是否((过多
        A.pop();
       
        if(A.empty());
        else
        {
            score=0;
        }
        cout << score;
    
        return;
    
    }
    
    int main() {
        SolveC();
            system("pause");
        return 0;
    }
  • 相关阅读:
    拦截器的配置和执行流程
    Restful风格数据获取
    layer弹窗插件官网
    HTTP知识1
    解决Navicat for MySQL 连接 Mysql 8.0.11 出现1251- Client does not support authentication protocol 错误
    MySQL8.0以上版本的安装与配置——详细教程
    python 练习题1(set 和len组合使用)
    性能测试基础知识1
    linux下配置python3详解
    ckplayer、swfobject的简单应用
  • 原文地址:https://www.cnblogs.com/lqerio/p/11708619.html
Copyright © 2011-2022 走看看