当前位置:网站首页>Stack bracket matching

Stack bracket matching

2020-11-07 20:19:00 Hi, Mr. Dong

// Determine whether the input bracket format is correct ( It's limited to brackets and brackets )

【 Input 】

  [[([()])]]

【 Output 】

  YES


#include<iostream>
#include<cstring>
using namespace std;
char b[101];
int top=0;

void push(char x){
  if(top<101){
    top++;
    b[top]=x;
    return;
  }
  cout<<" Stack failed "<<endl;

}


void pop(){
  if(top>0){
    top--;
    return;
  }
  cout<<" Stack out failed "<<endl;
}


int main(){
  char a[101];
  cin.getline(a,101);
  for(int i=0;i<strlen(a);i++){
    if(a[i]=='('||a[i]=='[') push(a[i]);
    else if(b[top]=='('&&a[i]==')') pop();
    else if(b[top]=='['&&a[i]==']') pop();
    else cout<<"NO"<<endl;
  }
  if(top==0)cout<<"YES"<<endl;
  return 0;
}

版权声明
本文为[Hi, Mr. Dong]所创,转载请带上原文链接,感谢