[NUDT2018银河之光ACM程序设计竞赛初赛] F: T28118 Brackets

题目描述

    Bracket is a kind of important punctuation. We can use it in many ways. However, sometimes it can confuse people if someone use it incorrectly. Now, you need to write a program to determine wether the usage of barcket is right.Give you a string of barcket. If the usage of barcket is right print "YES", if it's wrong, print "NO".

输入输出格式

输入格式:

    There are many test cases.Each case contains a string in one line.The length of the string will not exceed 100,000.

输出格式:

    For each case print "YES" or "NO"

输入输出样例

  
输入样例#1: 复制
()((()))()
(()()((()))))
输出样例#1: 复制
YES
NO




题意:给你一串小括号,判断是否合法;

    事实上,这串括号合法的条件就是'('和')'一样多,而且不出现类似")("的情况。那么,只要用一个计数器cnt,遇到'('cnt++,')'cnt--,再判断一下')'时cnt不等于零就好了。
    下面是AC代码:
#include <bits/stdc++.h>
using namespace std;
/*
    @第七只狐狸
*/

int main() 
{
    char s[500005];
    while(cin>>s)
    {
        int cnt=0,len=strlen(s);
        for(int i=0;i<len;i++)
        {
            if(s[i]=='(') cnt++;
            else if(s[i]==')') 
            {
                if(cnt==0) 
                {
                    cnt++;
                    break;
                }
                cnt--;
            }
        }
        if(cnt) cout<<"NO\n";
        else cout<<"YES\n";
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值