stack方式模拟,函数自己看
2023-08-02 18:45:03
发布于:上海
1阅读
0回复
0点赞
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cstring>
#include<cmath>
using namespace std;
char st[10001];
int top=0;
void push(char x){ st[++top]=x; }
void pop(){ top--; }
char Top(){ return st[top]; }
bool empty(){ return top==0; }
int size(){ return top; }
void clean(){ top = 0; }
void out(){	while(! empty()){ cout<<Top(); pop(); } }
int main(){
	char ans[10001];
	cin>>ans;
	for(int i=0;i<strlen(ans)-1;i++){
		if(ans[i]=='(' || ans[i]=='[' || ans[i]=='{' || ans[i]=='<'){
			push(ans[i]);
//			continue;
		}
		else if(ans[i]==')'){
			if(Top()=='(') pop();
			else{
				cout<<"NO";
				return 0;
			}
		}
		else if(ans[i]==']'){
			if(Top()=='[') pop();
			else{
				cout<<"NO";
				return 0;
			}
		}
		else if(ans[i]=='}'){
			if(Top()=='{') pop();
			else{
				cout<<"NO";
				return 0;
			}
		}
		else if(ans[i]=='<'){
			if(Top()=='>') pop();
			else{
				cout<<"NO";
				return 0;
			}
		}
		}
	if(!empty()) cout<<"NO";
	else cout<<"YES";
	return 0;
}
这里空空如也



有帮助,赞一个