File tree 1 file changed +51
-0
lines changed 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+ using ll = int64_t ;
5
+ using ff = long double ;
6
+
7
+ map<int , vector<string>> cache;
8
+ vector<string> solve (int N) {
9
+ if (N % 2 == 1 ) return {};
10
+ if (N == 0 ) {
11
+ return { " " };
12
+ }
13
+ if (cache.count (N)) {
14
+ return cache[N];
15
+ }
16
+
17
+ vector<string> ans;
18
+ for (auto const & s : solve (N-2 )) {
19
+ ans.push_back (" ()" + s);
20
+ ans.push_back (s + " ()" );
21
+ ans.push_back (" (" + s + " )" );
22
+ }
23
+ for (int i = 2 ; i < N; i += 2 ) {
24
+ int j = N - i;
25
+ auto ls = solve (i);
26
+ auto rs = solve (j);
27
+ for (auto const & lhs : ls) {
28
+ for (auto const & rhs : rs) {
29
+ ans.push_back (lhs + rhs);
30
+ }
31
+ }
32
+ }
33
+ sort (ans.begin (), ans.end ());
34
+ ans.erase (unique (ans.begin (), ans.end ()), ans.end ());
35
+
36
+ return cache[N] = ans;
37
+ }
38
+
39
+ int main () {
40
+ ios_base::sync_with_stdio (false );
41
+ cin.tie (0 ); cout.tie (0 );
42
+
43
+ int N;
44
+ cin >> N;
45
+ auto ans = solve (N);
46
+ for (auto const & p : ans) {
47
+ cout << p << endl;
48
+ }
49
+
50
+ return 0 ;
51
+ }
You can’t perform that action at this time.
0 commit comments