栈 例题 括号的匹配

检查括号层数最后匹不匹配即可
别忘了1~i段一定右括号的层数小于左括号 否则也要输出NO
样例:2*(x+y)/(1-x)@ YES

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char a[100010];
int n=1;
int top=0;
int main(){
	a[n]=getchar();
	while (a[n]!='@'){
		n++;
		a[n]=getchar();
	}
	for (int i=1;i<=n;i++){
		if (a[i]=='('){
			top++;
		}
		if (a[i]==')'){
			if (top>0) top--;
			else{
				printf("NO");
				return 0;
			}
		}
	}
	if (top!=0) printf("NO");
	else printf("YES");
	return 0;
}