zoukankan      html  css  js  c++  java
  • POJ 1095 Trees Made to Order (卡特兰数)

    Trees Made to Order
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 6290   Accepted: 3619

    Description

    We can number binary trees using the following scheme: 
    The empty tree is numbered 0. 
    The single-node tree is numbered 1. 
    All binary trees having m nodes have numbers less than all those having m+1 nodes. 
    Any binary tree having m nodes with left and right subtrees L and R is numbered n such that all trees having m nodes numbered > n have either Left subtrees numbered higher than L, or A left subtree = L and a right subtree numbered higher than R. 

    The first 10 binary trees and tree number 20 in this sequence are shown below: 

    Your job for this problem is to output a binary tree when given its order number. 

    Input

    Input consists of multiple problem instances. Each instance consists of a single integer n, where 1 <= n <= 500,000,000. A value of n = 0 terminates input. (Note that this means you will never have to output the empty tree.)

    Output

    For each problem instance, you should output one line containing the tree corresponding to the order number for that instance. To print out the tree, use the following scheme: 

    A tree with no children should be output as X. 
    A tree with left and right subtrees L and R should be output as (L')X(R'), where L' and R' are the representations of L and R. 
    If L is empty, just output X(R'). 
    If R is empty, just output (L')X. 

    Sample Input

    1
    20
    31117532
    0

    Sample Output

    X
    ((X)X(X))X
    (X(X(((X(X))X(X))X(X))))X(((X((X)X((X)X)))X)X)

    Source

    卡特兰数学习: http://www.cnblogs.com/jackge/archive/2013/05/19/3086519.html

    预处理卡特兰数之后,可以推出当前是多少个节点组成的二叉树,也知道是在当前节点所有排序中的名次

    然后开始递归,从右子树开始枚举,判断左右子树的节点个数,递归。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    long long C[30]={1};
    
    void Init(){    //预处理卡特兰数  
        for(int i=1;i<=25;i++)
            C[i]=C[i-1]*(4*i-2)/(i+1);
    }
    
    void solve(int k,int cnt){      //输出k个节点的并排在第cnt位的二叉树  
        if(k==1){   //只有一个节点,直接输出
            printf("X");
            return ;
        }
        if(cnt<=C[k-1]){    //排名很靠前,只有右子树  
            printf("X");
            printf("(");
            solve(k-1,cnt);
            printf(")");
        }else if(cnt>C[k]-C[k-1]){  //排名很靠后,只有左子树  
            printf("(");
            solve(k-1,cnt-(C[k]-C[k-1]));
            printf(")");
            printf("X");
        }else{
            int t=k-1,m;
            for(int i=t;i>=0;i--){  //判断左右子树各有多少个节点,更新名次  
                if(C[i]*C[t-i]<cnt)
                    cnt-=C[i]*C[t-i];
                else{
                    m=i;
                    break;
                }
            }
            printf("(");
            solve(t-m,cnt/C[m]+(cnt%C[m]!=0));  //递归左子树  
            printf(")X(");
            solve(m,(cnt-1)%C[m]+1);    //递归右子树
            printf(")");
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n,m;
        Init();
        while(~scanf("%d",&n) && n){
            for(int i=1;;i++){
                if(n>C[i])
                    n-=C[i];
                else{
                    m=i;
                    break;
                }
            }
            solve(m,n);
            printf("\n");
        }
        return 0;
    }
  • 相关阅读:
    再说Play!framework http://hsfgo.iteye.com/blog/806974
    PlayFramework 1 自定义标签 -- FastTags http://unmi.cc/category/javajee/playframework/
    [译] 第三十天:Play Framework
    你可以使用 play framework 做5件很爽的事情http://www.anool.net/?p=629
    Play常用代码片段 http://www.anool.net/?p=625
    Play 内置模板标签(1.2.3版本)http://www.anool.net/?p=617
    for what? while 与 until 差在哪?-- Shell十三问<第十三问>
    你要 if 还是 case 呢?-- Shell十三问<第十二问>
    > 与 < 差在哪?-- Shell十三问<第十一问>
    && 与 || 差在哪?-- Shell十三问<第十问>
  • 原文地址:https://www.cnblogs.com/jackge/p/3086526.html
Copyright © 2011-2022 走看看