Corn's new language——个人c++解

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 P P is a valid program

3. PQ PQ is a valid program, if both P P and Q Q 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 2 2, while " ((())()) ((())())" has a depth of 3 3. 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 100 100.

 

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.

 

Sample Input

(
)
()
(())
((())())
())
((

 

Sample Output

NO
NO
YES 1
YES 2
YES 3
NO
NO
 
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,j,n,l,k,m;
    char a[102];
    stack<char>s;
    while(scanf("%s",a)!=EOF)
    {
        m=0;
        k=0;
        while(!s.empty())
        {
            s.pop();
        }
        l=strlen(a);
            for(j=0;j<l;j++)
            {
                if(a[j]=='(')
                {
                    k=k+1;
                }
                if(a[j]==')')
                {
                    if(k>m)
                    {
                        m=k;
                    }
                    k=k-1;
                }
                if(s.empty())
                {
                    s.push(a[j]);
                }
            else
                if(s.top()+1==a[j]||s.top()+2==a[j])
                {
                    s.pop();
                }
                    else
                    {
                        s.push(a[j]);
                    }
            }
            if(s.empty())
            {
                printf("YES %d\n",m);
            }
            else
            {
                printf("NO\n");
            }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值