50 lines
1.0 KiB
C++
50 lines
1.0 KiB
C++
#include <iostream>
|
|
#include <stack>
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
bool first(char c){
|
|
if(c == '(' || c == '{' || c == '[') return true;
|
|
return false;
|
|
}
|
|
|
|
bool last(char c){
|
|
if(c == ')' || c == ']' || c == '}') return true;
|
|
return false;
|
|
}
|
|
|
|
bool match(char a, char b){
|
|
if(a == '(' && b == ')') return true;
|
|
if(a == '{' && b == '}') return true;
|
|
if(a == '[' && b == ']') return true;
|
|
return false;
|
|
}
|
|
|
|
bool isValid(const string& s){
|
|
if(s.empty()) return true;
|
|
if(last(s[0])) return false;
|
|
if(s.length() % 2 == 1) return false;
|
|
|
|
int len = s.length();
|
|
stack<char> st;
|
|
|
|
for(int i = 0; i < len; ++i){
|
|
if(first(s[i])) st.push(s[i]);
|
|
if(last(s[i])){
|
|
char top = st.top();
|
|
if(match(top, s[i])) st.pop();
|
|
else return false;
|
|
}
|
|
}
|
|
if(!st.empty()) return false;
|
|
return true;
|
|
}
|
|
|
|
int main(){
|
|
string s;
|
|
getline(cin, s);
|
|
cout << (isValid(s) ? "true" : "false") << endl;
|
|
return 0;
|
|
}
|