|
板凳
楼主 |
发表于 2020-1-31 11:23:50
|
只看该作者
- #include <bits/stdc++.h>
- using namespace std;
- const int catalan[18] = {1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790};
- int n;
- int nodes(int x)
- {
- int sum = 0;
- for (int i=1; i<=17; i++)
- {
- sum += catalan[i];
- if (sum>x) return i;
- }
- return 18;
- }
- void dfs(int node,int x)
- {
- if (node==0) return ;
- if (node==1)
- {
- cout<<"X";
- return ;
- }
- int temp_no = x;
- int left = 0,right = node - 1;
- while (1)
- {
- int temp;
- if ((temp = catalan[left]*catalan[right])>=temp_no)
- break;
- temp_no -= temp;
- left ++;
- right --;
- }
- if (left>0)
- {
- cout<<"(";
- dfs(left,(temp_no - 1)/catalan[right] + 1);
- cout<<")";
- }
- cout<<"X";
- if (right>0)
- {
- cout<<"(";
- dfs(right,(temp_no - 1)%catalan[right] + 1);
- cout<<")";
- }
- }
- int main()
- {
- cin>>n;
- int node = nodes(n);
- int temp = n;
- for (int i=1; i<node; i++)
- temp -= catalan[i];
- dfs(node,temp);
- return 0;
- }
复制代码 |
|