-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path괄호 변환.cpp
52 lines (49 loc) · 930 Bytes
/
괄호 변환.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
#include <string>
#include <vector>
using namespace std;
bool Correct(string s) {
int sum = 0;
for (int i = 0; i < s.length(); i++) {
if (s.at(i) == '(')
sum++;
else if (s.at(i) == ')')
sum--;
if (sum < 0)
return false;
}
return true;
}
string func(string s) {
if (s == "")
return "";
string u; string v;
int i = 0;
int left = 0; int right = 0;
for (i = 0; i < s.length(); i++) {
if (s.at(i) == '(')
left++;
else if (s.at(i) == ')')
right++;
if (left == right) {
u = s.substr(0, i + 1);
v = s.substr(i + 1);
break;
}
}
if (Correct(u))
return u + func(v);
else {
string temp = "(" + func(v) + ")";
u = u.substr(1, u.length() - 2);
for (int i = 0; i < u.length(); i++) {
if (u.at(i) == '(')
temp = temp + ')';
else
temp = temp + '(';
}
return temp;
}
}
string solution(string p) {
return func(p);
}