A

二次元勿喷  •  6个月前


include

include

include

using namespace std;

int priority(char c) {

int a = 0;
if (c == '<')
	a = 1;
if (c == '(')
	a = 2;
if (c == '[')
	a = 3;
if (c == '{')
	a = 4;
return a;

} void judge(string str) {

bool sign = true;
stack<char>s;
for (int i = 0; i < str.size(); i++) {
	if (str[i] == '<' || str[i] == '(' || str[i] == '[' || str[i] == '{') {
		char top;
		if (s.size() > 0)
			top = s.top();
		s.push(str[i]);
		if (s.size() >= 2) {
			if (priority(top) < priority(s.top())) {
				sign = false;
				break;
			}
		}
	} else {
		if (str[i] == '>' && s.size() > 0) {
			if (s.top() == '<')
				s.pop();
			else
				sign = false;
		} else if (str[i] == ')' && s.size() > 0) {
			if (s.top() == '(')
				s.pop();
			else
				sign = false;
		} else if (str[i] == '}' && s.size() > 0) {
			if (s.top() == '{')
				s.pop();
			else
				sign = false;
		} else if (str[i] == ']' && s.size() > 0) {
			if (s.top() == '[')
				s.pop();
			else
				sign = false;
		} else {
			sign = false;
			break;
		}
	}
}
//printf("%d \n", s.size());
if (sign && s.empty())
	cout << "YES" << endl;
else
	cout << "NO" << endl;

} int main() {

int n;
cin >> n;
string str;
for (int i = 0; i < n; i++) {
	cin >> str;
	judge(str);
}
return 0;

}


评论:

请先登录,才能进行评论