BUPT-homework/semester3/pset1/7-1-Balancing-Symbols.cpp

72 lines
1.3 KiB
C++
Raw Normal View History

2022-10-08 09:33:48 +08:00
#include <cstdio>
#include <iostream>
#include <pthread.h>
#include <stack>
#include <string>
#include <vector>
char getParen(const char &ch) {
if (ch == ']') {
return '[';
} else if (ch == ')') {
return '(';
} else {
return '{';
}
}
int main(void) {
std::string input;
std::cin >> input;
std::stack<char> st;
bool ans[4] = {false, false, false, false};
for (char ch : input) {
// scan for errors
// () [] {}
if (ch == '(' || ch == '[' || ch == '{') {
st.push(ch);
} else if (ch == ')' || ch == ']' || ch == '}') {
// check
if (!st.empty() && st.top() == getParen(ch)) {
// pop, nothing wrong
st.pop();
} else {
if (ch == ')') {
ans[1] = true;
} else if (ch == ']') {
ans[2] = true;
} else {
ans[3] = true;
}
ans[0] = true;
}
}
}
while (!st.empty()) {
if (st.top() == '(') {
ans[1] = true;
} else if (st.top() == '[') {
ans[2] = true;
} else {
ans[3] = true;
}
ans[0] = true;
st.pop();
}
if (ans[0] == false) {
printf("0");
} else {
for (int i = 1; i < 4; i++) {
if (ans[i] == true) {
printf("%d,", i);
}
}
}
return 0;
}