-
Notifications
You must be signed in to change notification settings - Fork 0
/
괄호_변환.cpp
63 lines (57 loc) · 1.11 KB
/
괄호_변환.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
#include <string>
#include <vector>
#include <stack>
using namespace std;
bool cmpc(string p) {
stack<char> st;
if (p[0] == ')') return false;
for (int i = 0; i < p.length(); i++) {
if (p[i] == ')') {
if (st.empty()) return false;
st.pop();
}
else st.push(p[i]);
}
return true;
}
bool cmpb(string p) {
int o = 0;
int c = 0;
for (int i = 0; i < p.length(); i++) {
if (p[i] == '(') o++;
else c++;
}
if (o == c) return true;
else return false;
}
string go(string p) {
if (p == "") return "";
for (int i = 1; i < p.length(); i += 2) {
string u = "";
string v = "";
for (int j = 0; j <= i; j++) u += p[j];
for (int j = i + 1; j < p.length(); j++) v += p[j];
if (!cmpb(u)) continue;
if (cmpc(u)) return u += go(v);
else {
string s = "";
string tmp = "";
s += '(';
s += go(v);
s += ')';
for (int i = 0; i < u.length(); i++) {
if (i == 0 || i == u.length() - 1) continue;
if (u[i] == '(') tmp += ')';
else tmp += '(';
}
s += tmp;
return s;
}
}
}
string solution(string p) {
string answer = "";
if (cmpc(p)) return p;
answer = go(p);
return answer;
}