码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive 3516 Exploring Pyramids (区间dp)

时间:2016-07-24 17:36:37      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
const int maxn = 310;
const ll mod = 1e9;
char s[maxn];
ll dp[maxn][maxn];

ll solve(int i, int j) {
    if(i == j) return 1;
    if(s[i] != s[j]) return 0;
    ll& ans = dp[i][j];
    if(ans >= 0) return ans;
    ans = 0;
    for(int k = i + 2; k <= j; k++) if(s[i] == s[k]) {
        ans = (ans + solve(i+1, k-1) * solve(k, j)) % mod;
    }
    return ans;
}
int main() {
    while(~scanf("%s", s)) {
        memset(dp, -1, sizeof(dp));
        cout << solve(0, strlen(s) - 1) << endl;
    }
}

 

UVALive 3516 Exploring Pyramids (区间dp)

标签:

原文地址:http://www.cnblogs.com/lonewanderer/p/5701031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!