zoukankan      html  css  js  c++  java
  • UVA 10303

    Problem D

    How Many Trees?

    Input: standard input

    Output: standard output

    Memory Limit: 32 MB

    A binary search tree is a binary tree with root k such that any node v in the left subtree of k has label (v) <label (k) and any node w in the right subtree of k has label (w) > label (k).

    When using binary search trees, one can easily look for a node with a given label x: After we compare x to the label of the root, either we found the node we seek or we know which subtree it is in. For most binary search trees the average time to find one of its n nodes in this way is O(log n).

    Given a number n, can you tell how many different binary search trees may be constructed with a set of numbers of size n such that each element of the set will be associated to the label of exactly one node in a binary search tree?

    Input and Output

    The input will contain a number 1 <= i <= 1000 per line representing the number of elements of the set. You have to print a line in the output for each entry with the answer to the previous question.

    Sample Input

     
    1
    2
    3

     

    Sample Output

    1
    2
    5

    题意:给定n个结点,求有几种2叉搜索树。

    思路:分别取第n个点做根节点。如此图


    代码:

    #include <stdio.h>
    #include <string.h>
    #define max(a,b) (a)>(b)?(a):(b)
    #define min(a,b) (a)<(b)?(a):(b)
    const int N = 1005;
    const int MAXBIGN = 1005;
    
    
    struct bign {
        int s[MAXBIGN];
        int len;
        bign() {
    		len = 1;
    		memset(s, 0, sizeof(s));
        }
    	
        bign operator = (const char *number) {
    		len = strlen(number);
    		for (int i = 0; i < len; i++)
    			s[len - i - 1] = number[i] - '0';
    		return *this;
        }
        bign operator = (const int num) {
    		char number[N];
    		sprintf(number, "%d", num);
    		*this = number;
    		return *this;
        }
    	
        bign (int number) {*this = number;}
        bign (const char* number) {*this = number;}
    	
        bign operator + (const bign &c){  
    		bign sum;
    		int t = 0;
    		sum.len = max(this->len, c.len);
    		for (int i = 0; i < sum.len; i++) {
    			if (i < this->len) t += this->s[i];
    			if (i < c.len) t += c.s[i];
    			sum.s[i] = t % 10;
    			t /= 10;
    		}
    		
    		while (t) {
    			sum.s[sum.len++] = t % 10;
    			t /= 10;
    		}
    		
    		return sum;  
        }
    	
    	bign operator * (const bign &c){  
    		bign sum; bign zero;
    		if (*this == zero || c == zero)
    			return zero;
    		int i, j;
    		sum.len = this->len + c.len;
    		for (i = 0; i < this->len; i++) {
    			for (j = 0; j < c.len; j ++) {
    				sum.s[i + j] += this->s[i] * c.s[j];
    			}
    		}
    		for (i = 0; i < sum.len; i ++) {
    			sum.s[i + 1] += sum.s[i] / 10;
    			sum.s[i] %= 10;
    		}
    		sum.len ++;
    		while (!sum.s[sum.len - 1]) {
    			sum.len --;
    		}
    		return sum;  
        }	
    	bign operator * (const int &num) {
    		bign c = num;
    		return *this * c;
    	}
    	bign operator / (const int &num) {
    		bign ans; int k = 0;
    		ans.len = len;
    		for (int i = ans.len - 1; i >= 0; i --) {
    			ans.s[i] = (k * 10 + s[i]) / num;
    			k = (k * 10 + s[i]) % num;
    		}
    		while (!ans.s[ans.len - 1]) {
    			ans.len --;
    		}
    		return ans;
    	}
        bign operator - (const bign &c) {
    		bign ans;
    		ans.len = max(this->len, c.len);
    		int i;
    		
    		for (i = 0; i < c.len; i++) {
    			if (this->s[i] < c.s[i]) {
    				this->s[i] += 10;
    				this->s[i + 1]--;
    			}
    			ans.s[i] = this->s[i] - c.s[i];
    		}
    		
    		for (; i < this->len; i++) {
    			if (this->s[i] < 0) {
    				this->s[i] += 10;
    				this->s[i + 1]--;
    			}
    			ans.s[i] = this->s[i];
    		}
    		while (ans.s[ans.len - 1] == 0) {
    			ans.len--;
    		}
    		if (ans.len == 0) ans.len = 1;
    		return ans;
        }
    	
        void put() {
    		if (len == 1 && s[0] == 0) {
    			printf("0");
    		} else {
    			for (int i = len - 1; i >= 0; i--)
    				printf("%d", s[i]);
    		}
        }
    	
        bool operator < (const bign& b) const {
    		if (len != b.len)
    			return len < b.len;
    		
    		for (int i = len - 1; i >= 0; i--)
    			if (s[i] != b.s[i])
    				return s[i] < b.s[i];
    			return false;
        }
        bool operator > (const bign& b) const { return b < *this; }
        bool operator <= (const bign& b) const { return !(b < *this); }
        bool operator >= (const bign& b) const { return !(*this < b); }
        bool operator != (const bign& b) const { return b < *this || *this < b;}
        bool operator == (const bign& b) const { return !(b != *this); }
    };
    
    bign f[1005];
    int n; 
    
    void init() {
    	f[1] = 1;
    	for (int i = 2; i <= 1000; i ++) {
    		f[i] = f[i - 1] * (4 * i - 2) / (i + 1);
    	}
    }
    
    int main() {
    	init();
    	while (~scanf("%d", &n) && n) {
    		f[n].put();
    		printf("
    ");
    	}
    	return 0;
    }
    


  • 相关阅读:
    Poj2155Matrix二维线段树
    二维树状数组模板
    PAT-1014 Waiting in Line (30 分) 优先队列
    PAT-1012 The Best Rank (25 分) 查询分数对应排名(包括并列)
    PAT-1003 Emergency (25 分) 最短路最大点权+求相同cost最短路的数量
    PAT-1001 A+B Format (20 分) 注意零的特例
    利用requests和BeautifulSoup爬取菜鸟教程的代码与图片并保存为markdown格式
    菜鸟教程上的设计模式代码合集
    用python将项目中的所有代码(或txt)合并在一个文件中
    POJ 2485 Prim 找最长的边
  • 原文地址:https://www.cnblogs.com/pangblog/p/3424164.html
Copyright © 2011-2022 走看看