题目:
给你一堆括号组成的字符串, 其长度就是字符长度,
其深度
问长度 为 n, 深度为 d 的串有多少种可能。
分析:
首先 Dp[L][D] 表示 长度为 L, 深度最大为 D 的字符串种数。
可以发现
Dp[L][D]=∑Dp[k][j−1]∗Dp[L−k−1][j]
那么答案就是
Ans=Dp[L][D]−Dp[L][D−1]
了。
然后数据小, 直接写了
Code:
import java.math.*;
import java.util.*;
public class Main {
public static BigInteger[][] Dp = new BigInteger[300][300];
public static void INIT() {
for(int i = 0; i < 300; ++i)
Dp[0][i] = new BigInteger("1");
for(int i = 1; i < 300; ++i) {
for(int j = 0; j < 300; ++j)
Dp[i][j] = new BigInteger("0");
}
for(int i = 1; i <= 151; ++i) {
for(int j = 1; j <= 150; ++j) {
for(int k = 0; k < i; ++k) {
Dp[i][j] = Dp[i][j].add(Dp[k][j-1].multiply(Dp[i-k-1][j]));
}
}
}
}
public static void main(String[] args) {
INIT();
Scanner cin = new Scanner(System.in);
int n, d;
while(cin.hasNext()) {
n = cin.nextInt();
d = cin.nextInt();
if(n % 2 == 1) System.out.println("1");
else {
n /= 2;
System.out.println(Dp[n][d].subtract(Dp[n][d-1]));
}
}
}
}