-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrakets.cpp
More file actions
58 lines (46 loc) · 1.03 KB
/
brakets.cpp
File metadata and controls
58 lines (46 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
class Stack {
private:
int top = -1;
char *storage;
public:
Stack(int size) {
this->storage = new char[size];
}
void push(char bracket) {
this->storage[++this->top] = bracket;
}
char pop(){
return this->storage[this->top--];
}
bool isEmpty() {
return this->top == -1;
}
};
bool isVPS(string brackets) {
Stack s = Stack(brackets.size());
for(int i = 0, len = brackets.size(); i < len; i++) {
if(brackets[i] == '(') {
s.push('(');
}else {
if(s.isEmpty()) {
return false;
}
s.pop();
}
}
return s.isEmpty();
}
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
string brackets;
cin >> brackets;
printf("%s\n", isVPS(brackets) ? "YES" : "NO");
}
return 0;
}