题目描述
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"
输入输出样例
题意:给你一串小括号,判断是否合法;
事实上,这串括号合法的条件就是'('和')'一样多,而且不出现类似")("的情况。那么,只要用一个计数器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;
}