标签:style class c code tar http
题目大意:给出一个字符串,问有多少种多叉树德前序遍历(这里每经过一个节点,该节点的值即要被算入,回溯的也要)满足该字符串。
解题思路:dp[i][j]表示从i到j的位置可以用多少种多叉树表示。转移方程:dp[i][j]=∑k=i+2jdp[i+1][k?1]?dp[k][j]。
#include <cstdio>
#include <cstring>
typedef long long ll;
const int N = 305;
const ll MOD = 1e9;
char s[N];
ll dp[N][N];
ll solve (int x, int y) {
if (x == y)
return dp[x][y] = 1;
if (s[x] != s[y])
return dp[x][y] = 0;
ll& ans = dp[x][y];
if (ans >= 0)
return ans;
ans = 0;
for (int i = x+2; i <= y; i++) {
if (s[x] == s[i])
ans = (ans + solve(x+1, i-1) * solve(i, y))%MOD;
}
return ans;
}
int main () {
while (scanf("%s", s) == 1) {
memset(dp, -1, sizeof(dp));
printf("%lld\n", solve(0, strlen(s)-1));
}
return 0;
}
uva 1362 - Exploring Pyramids(区间dp),布布扣,bubuko.com
uva 1362 - Exploring Pyramids(区间dp)
标签:style class c code tar http
原文地址:http://blog.csdn.net/keshuai19940722/article/details/26146683