-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa-551.cpp
More file actions
80 lines (57 loc) · 2.04 KB
/
UVa-551.cpp
File metadata and controls
80 lines (57 loc) · 2.04 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h> // here we have all the STL we need, including istringstream and ostringstream
#define ALL(x) x.begin(), x.end()
#define FAST std::cin.tie(0); ios::sync_with_stdio(false); std::cout.tie(0);
using namespace std;
#define min(a,b) (a < b) ? (a) : (b)
#define max(a,b) (a > b) ? (a) : (b)
#define vii vector<pair<int,int>>
bool isOpenningBracket(char c);
bool isClosingBracket(char c);
int main() {
string input;
while (cin >> input) {
int len = input.length(), i;
stack<char> match;
int count = 0;
for (i = 0; i < len; i++) {
count++;
if (isOpenningBracket(input[i])) {
if (i+1 < len && input[i+1] == '*' && input[i] == '(') {
match.push('*');
i++;
}
else
match.push(input[i]);
}
else if (isClosingBracket(input[i]) || (input[i] == '*' && i+1 < len && input[i+1] == ')')) {
if (match.empty()) {
break;
}
char top = match.top();
if (input[i] == '*' && i+1 < len && input[i+1] == ')' && top == '*') {
match.pop();
i++;
}
else if (top == '(' && input[i] == ')') match.pop();
else if (top == '{' && input[i] == '}') match.pop();
else if (top == '[' && input[i] == ']') match.pop();
else if (top == '<' && input[i] == '>') match.pop();
else {
break;
}
}
}
if (i < len || !match.empty()) {
if (i >= len) count++;
cout << "NO " << count << endl;
}
else cout << "YES" << endl;
while (!match.empty()) match.pop();
}
}
bool isOpenningBracket(char c) {
return (c == '{' || c == '[' || c == '(' || c == '<');
}
bool isClosingBracket(char c) {
return (c == '}' || c == ']' || c == ')' || c == '>');
}