p1739题解

· · 题解

首先我们考虑左括号的数量一定严格大于等于右括号的数量。    
其次我们用数组保存每一个字符,O(N)算法绝对能过,所以我们可以放心枚举每个字符(但是右括号大于左括号时return 0)     
下附AC代码
#include<bits/stdc++.h>                           
using namespace std;                            
char ch[255];                                 
int main(void){                             
    scanf("%s",ch);                       
    int len = strlen(ch),a=0,b=0;               
    for(int i=0;i<len;i++){                                                                                
        if(ch[i]=='(')                                    
            a++;                                    
        if(ch[i]==')')                      
            b++;                   
        if(b>a){                       
            cout<<"NO";                          
            return 0;                       
        }
    }                        
    if(a==b)                            
        cout<<"YES";                        
    else                 
        cout<<"NO";                     
    return 0;
}