UVa Problem Solution: 10157 - Expressions

本文介绍了一种用于计算特定长度和最大深度下正确括号表达式的数量的算法。通过递归公式定义了状态转移方程,并实现了动态规划求解方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Let n denotes the length of the expression, d denotes the depth at most, and the count is f(n,d). Obviously, f(n,d) = 0 when n is odd. For other conditions, let us consider where we can find the corresponding parenthesis of the leftmost one. It can be only found at the even places. For place 2, this yields f(2, d-1)*f(n-2,d) ways of correctly built expressions. And for place 4, the number will be f(4, d-1)*f(n-4,d). So we can draw this recurrence from the above observation
    f(n,d) = E{i:2->n-2,i+=2:f(i,d-1)*f(n-i,d)}
and and base cases are
    f(0,d) = 1 where d >= 0

BTW, I develop a tool to post my code to the UVa online judge automatically. This tool will also replace the local #include's with its real content when uploading the code to the server. So, I haven't  to paste the code any more. All the included files could be found on this site.

Code:
  1. /***************************************************************************
  2.  *   Copyright (C) 2008 by Liu Kaipeng                                     *
  3.  *   LiuKaipeng at gmail dot com                                           *
  4.  ***************************************************************************/
  5. /* @JUDGE_ID 00000 10157 C++ "Expressions" */
  6. #include <algorithm>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <deque>
  10. #include <fstream>
  11. #include <iostream>
  12. #include <limits>
  13. #include <list>
  14. #include <map>
  15. #include <queue>
  16. #include <set>
  17. #include <stack>
  18. #include <string>
  19. #include <vector>
  20. #include <iterator>
  21. #include <string>
  22. #include "big_unsigned.hpp"
  23. using namespace std;
  24. int const nsize = 301;
  25. int const dsize = 151;
  26. big_unsigned numbers[nsize][dsize], results[nsize][dsize];
  27. /*
  28.  * Let n denotes the length of the expression, d denotes the depth at most,
  29.  * and the count is f(n,d). Obviously, f(n,d) = 0 when n is odd. For other
  30.  * conditions, let us consider where we can find the corresponding
  31.  * parenthesis of the leftmost one. It can be only found at the even places.
  32.  * For place 2, this yields f(2, d-1)*f(n-2,d) ways of correctly built
  33.  * expressions. And for place 4, the number will be f(4, d-1)*f(n-4,d).
  34.  * So we can draw this recurrence from the above observation
  35.  *   f(n,d) = E{i:2->n-2,i+=2:f(i,d-1)*f(n-i,d)}
  36.  * and and base cases are
  37.  *   f(0,d) = 1 where d >= 0
  38.  */
  39. void gen_numbers()
  40. {
  41.   for (int d = 0; d < dsize; ++d)
  42.     numbers[0][d] = 1;
  43.   for (int n = 2; n < nsize; n += 2)
  44.     for (int d = 1; d < dsize ; ++d)
  45.       for (int i = 2; i <= n; i += 2)
  46.         numbers[n][d] += numbers[i-2][d-1] * numbers[n-i][d];
  47.   for (int n = 1; n < nsize; ++n)
  48.     for (int d = 1; d < dsize; ++d)
  49.       results[n][d] = numbers[n][d] - numbers[n][d-1];
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. #ifndef ONLINE_JUDGE
  54.   freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
  55.   freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
  56. #endif
  57.   gen_numbers();
  58.   for (int n, d; cin >> n >> d; cout << results[n][d] << '/n') {}
  59.   return 0;
  60. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值