首页 > 其他 > 详细

Poj2795Exploring PyramidsDp

时间:2014-10-11 11:08:46      阅读:240      评论:0      收藏:0      [点我收藏+]

题意:给出一个字符串。问有多少个满足以下条件的树

从原点开始尽可能左走,不行就回溯,其路径符合给出字符串。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <math.h>
using namespace std;

typedef long long LL;
const LL mod = 1000000000;
const LL maxn = 300 + 10;
LL dp[maxn][maxn];
char s[maxn];
LL gao(LL i, LL j)
{
    if (~dp[i][j]) return dp[i][j];
    if (i == j) return dp[i][j] = 1;
    if (s[i] != s[j]) return dp[i][j] = 0;
    LL ans = 0;
    for (LL x = i + 2; x <= j; x++){
        if (s[i] != s[x]) continue;
        ans += gao(i + 1, x - 1) * gao(x, j);
        ans %= mod;
    }
    return dp[i][j] = ans;
}

int main()
{
    while (cin >> s){
        LL len = strlen(s);
        memset(dp, -1, sizeof(dp));
        cout << gao(0, len - 1)<<endl;
    }
    return 0;
}

 

Poj2795Exploring PyramidsDp

原文:http://www.cnblogs.com/yigexigua/p/4018407.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!