Information
- ID
- 296
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 7
- Tags
- # Submissions
- 180
- Accepted
- 37
- Uploaded By
#include <iostream>
#include <map>
#include <stack>
#include <cmath>
#include <string>
using namespace std;
map<char, int> m;
int main05(void) {
m['<'] = 1; m['>'] = -1;
m['('] = 2; m[')'] = -2;
m['['] = 3; m[']'] = -3;
m['{'] = 4; m['}'] = -4;
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
stack<char> s;
string str;
cin >> str;
bool flag = true;// 默认是 YES
for (int j = 0; j < str.size(); j++) {
if (s.empty()) {
s.push(str[j]);
}
else {
int ch = s.top();
if (m[ch] < m[str[j]]) {
flag = false;
break;
}
else if(m[ch] + m[str[j]] == 0) {
s.pop();
}
else {
s.push(str[j]);
}
}
}
if (flag && s.empty()) {
cout << "YES" << endl;
}
else if (flag && !s.empty()) {
cout << "NO" << endl;
}
else if (flag == false) {
cout << "NO" << endl;
}
}
return 0;
}
By signing up a 追梦算法网 universal account, you can submit code and join discussions in all online judging services provided by us.