其实是这道题的变式(某港带同学的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;
}

LeetCode 856. Score of Parentheses 括号的分数的更多相关文章

  1. Leetcode 856. Score of Parentheses 括号得分(栈)

    Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...

  2. [LeetCode] Score of Parentheses 括号的分数

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  3. LC 856. Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  4. 【LeetCode】856. Score of Parentheses 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 递归 计数 日期 题目地址:https://le ...

  5. 【LeetCode】22. Generate Parentheses 括号生成

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...

  6. 856. Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  7. LeetCode 856. 括号的分数(Score of Parentheses)

    856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A ...

  8. LeetCode:括号的分数【856】

    LeetCode:括号的分数[856] 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A 和 B 是平衡括号字符串. (A) ...

  9. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

随机推荐

  1. 实现简易版德州扑克|学习麻瓜编程以项目为导向入门前端 HTML+CSS+JS

    实现简易版德州扑克 1.先上达到网页效果图(简易版德州扑克) 网页分为发牌区和牌池,上面为发牌区,下面是牌池区 2. 代码实现 2.1 HTML和JS代码 ` <link rel="s ...

  2. Jmeter二次开发——自定义函数

    在之前的博文中,Jmeter二次开发--基于Java请求,已介绍了Jmeter二次开发的基础情况,上次分享的是java请求开发,今天来分享下Jmeter中的函数开发.聊到Jmeter的函数,知道Jme ...

  3. linux中的虚拟环境工具

    1.虚拟环境工具的学习 python的虚拟环境,其实就是在机器上,方便的创建出多个解释器,每个解释器运行一个项目,互相之间不受影响 2.virtualenv工具,可以方便的创建,使用,删除也很方便 3 ...

  4. JavaScript——深入了解this

    前言 我曾以为func()其实就是window.func() function func(){ console.log('this : ' + this); } func();//this : [ob ...

  5. Failed to start LSB: starts php-fpm

    跟nginx一样都是进程占用,记录下 [root@localhost pazzn]# systemctl status php-fpm.service ● php-fpm-72.service - L ...

  6. TCP/IP中的Payload概念以及由此引申出的一些问题

    TCP报文一次性最大运输的货物量(Payload),大体可以这么来计算: IP报文头长度  +  TCP报文头长度  +  Payload长度  ≤ MTU 即左边的三者之和,要小于等于右边MTU的长 ...

  7. windows.open、 window.location.href

    windows.open("URL","窗口名称","窗口外观设定");打开新窗口,window对象的方法 不一定打开新窗口,只要有窗口的名 ...

  8. Java——变量类型

    Java变量类型: 在Java中,所有的变量在使用前必须声明.格式: type identifier [ = value ][, identifier [ =value]-.]; type为Java数 ...

  9. 编程排序的一个excel-据说玩的好的,年薪50万了。只是你没在对的地方

    是一个excel 下面是百度网盘分享地址; http://pan.baidu.com/s/1kTwqRfL    

  10. Kubernetes --(k8s)Job、CronJob

    Job https://www.kubernetes.org.cn/job https://www.kubernetes.org.cn/cronjob Job负责批量处理短暂的一次性任务 (short ...