-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodingTree.cpp
More file actions
46 lines (41 loc) · 1.14 KB
/
EncodingTree.cpp
File metadata and controls
46 lines (41 loc) · 1.14 KB
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
//http://community.topcoder.com/stat?c=problem_solution&cr=144400&rd=7995&pm=4700
//BST tree
// Single Round Match 261 Round 1 - Division I, Level Two
#include <string>
using namespace std;
int ntrees[20];
string f(int number, int index,char aa)
{
if ( number == 0 ) return "";
int root = 0;
//find the root node
while( ntrees[root] * ntrees[number - 1 - root] <= index ){
index -= ntrees[root] * ntrees[number - 1 - root];
++root;
}
string s;
s += char(aa + root);
s += f(root, index / ntrees[number - 1 -root], aa );
s += f(number - 1 - root, index % ntrees[number - 1 -root],aa + root + 1);
return s;
}
string EncodingTrees(int number, int index )
{
index--;
ntrees[0] = 1;
for ( int i = 1; i <= 19; i++ ){
ntrees[i] = 0;
for ( int j = 0; j <= i - 1; j++ ){
ntrees[i] += ntrees[j] * ntrees[i - 1 - j];
}
}
if ( index < 0 || index >= ntrees[number])
//out of range
return "";
return f(number, index, 'a');
}
int main()
{
string result = EncodingTrees(12,121212);
return 0;
}