2017年上海金马五校程序设计竞赛(网上资格赛)Problem A : Corn's new language

本文介绍了一种新颖的编程语言及其语法规则,并提供了一个用于计算该语言程序最大深度的C++实现。通过具体示例,文章解释了如何确定一个程序是否有效及如何计算其最大深度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Corn is going to promote programming in the campus, so he wants to add a lot of interesting ideas to make programming more attractive. One task he is working on is to develop a new programming language because he thinks all existing ones are too simple to him. The syntax rules of the language are:

 

1. ()() is a valid program

2. (P)(P) is a valid program, if PP is a valid program

3. PQPQ is a valid program, if both PP and QQ are valid programs

 

Corn wants a compiler for the language. For a program, if it is valid, the compiler should print the max depth in the program. For example "(())(())" has a depth of 22, while "((())())((())())" has a depth of 33. Formally, the max depth is the max number of unmatched open brackets in any prefix.

 

Input

Each case is a non-empty string in a single line, the string will only contain '((' or '))', its length will be no more than 100100.

 

Output

For each case, output "YES" and the integer required above if the program is valid, separated with a space. Or "NO" if the program is invalid.



这个题真的挺简单的,但是我被卡了好久,微笑,记得注意这种情况。 ())(  是no微笑

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main(){
	char str[105];
	while(scanf("%s",&str)!=EOF){
		bool is=true;
		int higt = 0, left = 0,right = 0,flag = 0;
		int u = strlen(str);
		for(int i = 0;i < u; i++){
			if(str[i]=='('){
				left++;
				flag++;
			}else if(str[i]==')'){
				right++;
				flag--;
			}
			if(flag<0) is=false;
			if(flag>higt) higt = flag;
		}
		if(is)
		{
			if(left == right){
			printf("YES %d\n",higt);
		}else if(left!=right){
			printf("NO\n");
		}
		}
		else printf("NO\n");
	}
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_我走路带风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值