2020牛客寒假算法基础集训营4 B:括号序列
B : 括号序列
考察点 : 栈
坑点 : 只有栈空时才是合法的
Code:
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
string str;
stack<char>stacks;
int main(void) {
cin >> str;
bool flag = true;
for(int i = 0; i < str.length(); i ++) {
if(stacks.size() == 0) {
stacks.push(str[i]);
continue;
}
if(str[i] == '(' || str[i] == '{' || str[i] == '[') {
stacks.push(str[i]);
} else if(str[i] == ')' && stacks.top() == '(' && stacks.size() != 0) {
stacks.pop();
} else if(str[i] == '}' && stacks.top() == '{' && stacks.size() != 0) {
stacks.pop();
} else if(str[i] == ']' && stacks.top() == '[' && stacks.size() != 0) {
stacks.pop();
} else {
flag = false;
break;
}
}
if(flag && stacks.size() == 0) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
如果说年轻人未来是一场盛宴的话,那么我首先要有赴宴的资格。