forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path179.cpp
More file actions
42 lines (39 loc) · 683 Bytes
/
179.cpp
File metadata and controls
42 lines (39 loc) · 683 Bytes
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
//ac
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cin >> str;
int len = str.length();
int num = 0;
int ret = -1;
for (int i = len - 1; i >= 0; --i) {
if (str[i] == '(') {
++num;
if (num < 0) {
ret = i;
break;
}
} else {
--num;
}
}
if (ret == -1) cout << "No solution" << endl;
else {
for (int i = 0; i < ret; ++i) {
cout << str[i];
}
cout << ')';
int sl = 0, sr = 0;
for (int i = 0; i < ret; ++i) {
if (str[i] == '(') ++sl;
else ++sr;
}
++sr;
for (int i = 0; i < len / 2 - sl; ++i) cout << '(';
for (int i = 0; i < len / 2 - sr; ++i) cout << ')';
cout << endl;
}
return 0;
}