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