文章

68

粉丝

691

获赞

24

访问

546.5k

头像
题目坑点
P1067 中山大学2019年机试题
发布于2020年5月10日 12:10
阅读数 11.1k

注意括号的嵌套,同级是可以互相嵌套的{{}}是对的

#include<iostream>
#include<stack>
#include<string>
#include<string.h>
using namespace std;

bool cmp(char s1, char s2) {
	if (s1 == '<')s1 = 20;
	if (s2 == '<')s2 = 20;
	return s1 <= s2;
}

int main() {
	int n; cin >> n; string s; cin.get();
	for (int i = 0; i < n; i++) {
		getline(cin, s); stack<char> sta; int sign = 1;
		for (unsigned k = 0; k < s.size() && sign; k++) {
			char t = s[k];
			if (t == '{' || t == '[' || t == '(' || t == '<') {
				char tt;
				if (!sta.empty())tt = sta.top();
				else tt = '{' + 1;
				if (cmp(t, tt))sta.push(t);//满足次序要求,后嵌套的优先级低
				else sign = 0;
			}
			else if (sta.empty())sign = 0;
			else {
				char tt = sta.top(); sta.pop();
				if (tt == '{'&&t == '}')continue;
				if (tt == '('&&t == ')')continue;
				if (tt == '<'&&t == '>')continue;
				if (tt == '['&&t == ']')continue;
				sig...
登录查看完整内容


登录后发布评论

2 条评论
别再熬夜 VIP
2021年1月17日 21:38

我说怎么只过20%。。。

赞(0)
别再熬夜 VIP
2021年1月17日 21:38

我说怎么只过20%。。。

赞(0)