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;
}
posted @ 2020-02-13 10:09  IceSwords  阅读(108)  评论(0编辑  收藏  举报