-
Notifications
You must be signed in to change notification settings - Fork 0
/
4949.cpp
74 lines (66 loc) · 1.25 KB
/
4949.cpp
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
/* 백준 4949 균형잡힌 세상
자료구조, 문자열, 스택 */
#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack<char>s;
bool IsLeftBraket(string str, int idx) {
if (str[idx] == '(' || str[idx] == '[')
return true;
return false;
}
bool IsRightBraket(string str, int idx) {
if (str[idx] == ')' || str[idx] == ']')
return true;
return false;
}
bool Matching(string str, int idx) {
if (s.empty())
return false;
if (str[idx] == ')') {
if (s.top() == '(') {
s.pop();
return true;
}
else
return false;
} else if (str[idx] == ']') {
if (s.top() == '[') {
s.pop();
return true;
} else {return false;}
}
return false;
}
bool IsBalancedSentence(string str) {
int idx = 0;
while (str[idx]) {
if (IsLeftBraket(str, idx)) {
s.push(str[idx]);
} else if (IsRightBraket(str, idx)) {
if (!Matching(str, idx)) {
while (!s.empty())
s.pop();
return false;
}
}
idx++;
}
if (str[str.size() - 1] == '.' && s.empty()) {
return true;
} else {return false;}
}
int main(void) {
string str;
while (1) {
getline(cin, str);
if (str == ".")
break;
if (IsBalancedSentence(str)) {
cout << "yes" << endl;
} else {cout << "no" << endl;}
while (!s.empty())
s.pop();
}
}