UVA题目10157 - Expressions(DP+高精度)

原创 2015年11月18日 15:56:11
Let X be the set of correctly built parenthesis expressions. The elements of X are strings consisting only
of the characters ‘(’ and ‘)’. The set X is defined as follows:
• an empty string belongs to X
• if A belongs to X, then (A) belongs to X
• if both A and B belong to X, then the concatenation AB belongs to X.
For example, the following strings are correctly built parenthesis expressions (and therefore belong
to the set X):
()(())()
(()(()))
The expressions below are not correctly built parenthesis expressions (and are thus not in X):
(()))(()
())(()
Let E be a correctly built parenthesis expression (therefore E is a string belonging to X).
The length of E is the number of single parenthesis (characters) in E.
The depth D(E) of E is defined as follows:
D(E) =



0 if E is empty
D(A) + 1 if E = (A), and A is in X
max(D(A), D(B)) if E = AB, and A, B are in X
For example, the length of “()(())()” is 8, and its depth is 2. What is the number of correctly
built parenthesis expressions of length n and depth d, for given positive integers n and d?
Write a program which
• reads two integers n and d
• computes the number of correctly built parenthesis expressions of length n and depth d;
Input
Input consists of lines of pairs of two integers - n and d, at most one pair on line, 2 ≤ n ≤ 300,
1 ≤ d ≤ 150.
The number of lines in the input file is at most 20, the input may contain empty lines, which you
don’t need to consider.
Output
For every pair of integers in the input write single integer on one line - the number of correctly built
parenthesis expressions of length n and depth d.
Note: There are exactly three correctly built parenthesis expressions of length 6 and depth 2:
(())()
()(())
(()())
Sample Input
6 2
300 150
Sample Output
3

1

题目大意:n个括号,深度为d的种类有多种,

分成两部分,dp[i][j]表示i对,深度最多为j的有多少种,把括号分为两部分,一部分深度不超过j,一部分不超过j-1

转移方程dp[i][j]=dp[k][j-1]*dp[i-k-1][j]+dp[i][j];

深度的话就减一下就好了dp[n/2][m]-dp[n/2][m-1];

ac代码

16453524 10157 Expressions Accepted C++ 4.216 2015-11-18 07:47:56
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int M=155;
const int N=105;
struct bign {
    int len, sex;
    int s[N];

    bign() {
        this -> len = 1;
        this -> sex = 0;
        memset(s, 0, sizeof(s));
    }

    bign operator = (const char *number) {
        int begin = 0;
        len = 0;
        sex = 1;
        if (number[begin] == '-') {
            sex = -1;
            begin++;
        }
        else if (number[begin] == '+')
            begin++;

        for (int j = begin; number[j]; j++)
            s[len++] = number[j] - '0';
    }

    bign operator = (int number) {
        char string[N];
        sprintf(string, "%d", number);
        *this = string;
        return *this;
    }

    bign (int number) {*this = number;}
    bign (const char* number) {*this = number;}

    bign change(bign cur) {
        bign now;
        now = cur;
        for (int i = 0; i < cur.len; i++)
            now.s[i] = cur.s[cur.len - i - 1];
        return now;
    }

    void delZore() {    // 删除前导0.
        bign now = change(*this);
        while (now.s[now.len - 1] == 0 && now.len > 1) {
            now.len--;
        }
        *this = change(now);
    }

    void put() {    // 输出数值。
        delZore();
        if (sex < 0 && (len != 1 || s[0] != 0))
            cout << "-";
        for (int i = 0; i < len; i++)
            cout << s[i];
    }

    bign operator + (const bign &cur){
        bign sum, a, b;
        sum.len = 0;
        a = a.change(*this);
        b = b.change(cur);

        for (int i = 0, g = 0; g || i < a.len || i < b.len; i++){
            int x = g;
            if (i < a.len) x += a.s[i];
            if (i < b.len) x += b.s[i];
            sum.s[sum.len++] = x % 10;
            g = x / 10;
        }
        return sum.change(sum);
    }

    bign operator * (const bign &cur){
        bign sum, a, b;
        sum.len = 0;
        a = a.change(*this);
        b = b.change(cur);

        for (int i = 0; i < a.len; i++){
            int g = 0;

            for (int j = 0; j < b.len; j++){
                int x = a.s[i] * b.s[j] + g + sum.s[i + j];
                sum.s[i + j] = x % 10;
                g = x / 10;
            }
            sum.len = i + b.len;

            while (g){
                sum.s[sum.len++] = g % 10;
                g = g / 10;
            }
        }
        return sum.change(sum);
    }

    bign operator - (const bign &cur) {
        bign sum, a, b;
        sum.len = len;
        a = a.change(*this);
        b = b.change(cur);

        for (int i = 0; i < b.len; i++) {
            sum.s[i] = a.s[i] - b.s[i] + sum.s[i];
            if (sum.s[i] < 0) {
                sum.s[i] += 10;
                sum.s[i + 1]--;
            }
        }
        for (int i = b.len; i < a.len; i++) {
            sum.s[i] += a.s[i];
            if (sum.s[i] < 0) {
                sum.s[i] += 10;
                sum.s[i + 1]--;
            }
        }
        return sum.change(sum);
    }
};
bign dp[M][M];
void DP()
{
    int i,j,k;
    for(i=0;i<=150;i++)
        dp[0][i]=1;
    for(i=1;i<=150;i++)
    {
        for(j=1;j<=150;j++)
        {
            for(k=0;k<i;k++)
            {
                dp[i][j]=dp[k][j-1]*dp[i-k-1][j]+dp[i][j];
            }
        }
    }
}
int main()
{
    int n,m;
    DP();
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        bign sum=dp[n/2][m]-dp[n/2][m-1];
        sum.put();
        printf("\n");
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

UVa 10157 Expressions (组合数学&高精度)

10157 - Expressions Time limit: 10.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlin...
  • synapse7
  • synapse7
  • 2013年10月18日 12:35
  • 1491

UVA 10157 Expressions

题意:老是RE,看了题解:点击打开链接 #include #include using namespace std; const int LEN=100; const int NMAX=300;...
  • u011345136
  • u011345136
  • 2013年11月02日 19:10
  • 636

UVA 10157 - Expressions(dp卡特兰数高精度)

Problem A: EXPRESSIONS  Let X be the set of correctly built parenthesis expressions. The elem...
  • u011217342
  • u011217342
  • 2013年11月20日 22:58
  • 1035

uva 10157 - Expressions(dp)

题目链接:uva 10157 - Expressions 题目大意:给出n和d,表示说有n/2对括号,要求组成深度为d的表达式,问有多少种。 解题思路:一开始想说,每增加一对括号无非是加在最前...
  • u011328934
  • u011328934
  • 2013年11月06日 17:03
  • 1091

UVa 10157 - Expressions

题目:给你n个括号,求合法的匹配中,深度不超过d的组合数。 分析:组合,计数,dp,大整数。          这个题目很像卡塔兰数,不过深度有限制,可以利用卡塔兰数的递推公式求解;         ...
  • mobius_strip
  • mobius_strip
  • 2014年09月14日 14:16
  • 854

uva 10157 expressions

Problem A: EXPRESSIONS  Let X be the set of correctly built parenthesis expressions. The el...
  • chengouxuan
  • chengouxuan
  • 2012年01月17日 20:11
  • 363

uva 10157 Expressions

uva 10157 Expressions
  • tengfei461807914
  • tengfei461807914
  • 2017年08月03日 16:13
  • 98

UVa Problem Solution: 10157 - Expressions

Let n denotes the length of the expression, d denotes the depth at most, and the count is f(n,d). Ob...
  • liukaipeng
  • liukaipeng
  • 2008年12月03日 01:20
  • 2552

UVa:10157 Expressions(动态规划+大数)

这个题和http://blog.csdn.net/kkkwjx/article/details/20038467 基本一样。只不过数据更强一点,把数组改大之后就能AC。 我这里尝试了Java的大数,效...
  • kkkwjx
  • kkkwjx
  • 2014年03月18日 22:24
  • 508

Uva 10157 - Expressions 解题报告(递推)

Problem A: EXPRESSIONS  Let X be the set of correctly built parenthesis expressions. The elem...
  • kbdwo
  • kbdwo
  • 2014年04月02日 09:39
  • 714
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA题目10157 - Expressions(DP+高精度)
举报原因:
原因补充:

(最多只允许输入30个字)