Gym 101334E dp

分析:

这一题给出的遍历的点的序列,不是树的中序遍历,前序遍历,只要遇到一个节点就打印一个节点。关键点就在,这个序列的首字母和尾字母一定要相同,因为最终都会回到根节点,那么每一个子树也一样。

状态:

d[i][j]表示i至j的状态数

d[i][j]= d[i][j]=(d[i][j]+dp(i,k)*dp(k+1,j-1)%mod)%mod;

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=300+5;
const ll mod=1e9;
char s[maxn];
ll d[maxn][maxn];

ll dp(int i,int j)
{
    if(i>j) return 0;
    if(d[i][j]!=-1) return d[i][j];
    if(s[i]!=s[j]) return d[i][j]=0;
    if(i==j) return d[i][j]=1;
    d[i][j]=0;
    for(int k=i;k<j;k++)
        d[i][j]=(d[i][j]+dp(i,k)*dp(k+1,j-1)%mod)%mod;
    return d[i][j];
}

int main()
{
    freopen("exploring.in","r",stdin);
    freopen("exploring.out","w",stdout);
    while(~scanf("%s",s))
    {
        memset(d,-1,sizeof(d));
        printf("%I64d\n",dp(0,strlen(s)-1));
    }
    return 0;
}
时间: 2024-10-09 15:32:10

Gym 101334E dp的相关文章

Gym - 101334E 多叉树遍历

题意:给定一个字符串,求有多少种树与之对应,对应方式是,每次遍历左节点,没有了,就回溯: 分析:d[i,j] = sum(d[i+1,k-1],d[k,j]) (str[i]==str[k]); 坑点是数组竟然要long long 不然会超时,神奇: 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 300+5; 6 const int mod = 1000000000; 7 char str[ma

Loppinha, the boy who likes sopinha Gym - 101875E (dp,记忆化搜索)

https://vjudge.net/contest/299302#problem/E 题意:给出一个01 0101串,然后能量计算是连续的1就按1, 2, 3的能量加起来.然后给出起始的能量,求最少减掉几个一是起始的能量不被消耗完. 思路:不能用贪心,比如11111,按理说拿一个最好是中间分开,但是那两次的这种情况下应该是要把第二个和第四个拿掉来最小 所以要用记忆化搜索或dp: 记忆化搜索 #include<bits/stdc++.h> using namespace std; const

dp+分类讨论 Gym 101128E

题目链接:http://codeforces.com/gym/101128 感觉这个人写的不错的(我只看了题目大意):http://blog.csdn.net/v5zsq/article/details/61428924 Description n个小木条,一段前面有一个小箭头,给出第一个小木条的非箭头端端点横坐标以及每个小木条箭头端的坐标,现在要从下往上把这n'个木条按顺序叠放好,要求相邻两个小木条必须有一个共同端点且有交叠部分,问小木条有多少种放法 Input 第一行一整数n表示木条数量,之

Codeforces Gym 100676G Training Camp 状压dp

http://codeforces.com/gym/100676 题目大意是告诉你要修n门课,每门课有一个权值w[i], 在第k天修该课程讲获得k*w[i]的学习点数,给出了课程与先修课程的关系,要修该课程必须修完先修课程.问最多能学到多少点数. 非常简单的一道状压dp(一开始我还误导队友写成两维的去了 T^T); dp[s] : s 的二进制存放的是已经选择的课程,在该状态下的能获得的最大的点数. 这时如果再学一门课程k,将转移到状态ss (s | (1 << k) ) ,能否转移需要判断合

Codeforces GYM 100114 D. Selection 线段树维护DP

D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description When selecting files in an application dialog, Vasya noted that he can get the same selection in different ways. A simple mouse click selects a sing

Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Robots are becoming more and more popular. They are used nowadays not only in manufacturing plants, but also at home. One programmer wit

codeforces Gym 100500H A. Potion of Immortality 简单DP

Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description Noura Boubou is a Syrian volunteer at ACM ACPC (Arab Collegiate Programming Contest) since 2011. She graduated from Tishreen Un

Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度

Problem D. Dinner ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description A group of k students from Cooking University living in the campus decided that each day of the semester one of them will p

树形DP Gym 100496H House of Representatives

题目传送门 1 /* 2 题意:寻找一个根节点,求min f(u) = ∑ρ(v, u) * p(v).ρ(v, u)是u到v的距离,p(v)是v点的权值 3 树形DP:先从1出发遍历第一次,sum[u]计算u到所有子节点v的路径权值(之后的点路径有叠加,所以先把路径权值加后*w), 4 计算f[u](缺少u节点以上的信息).然后再遍历一遍,之前是DFS从下往上逆推,现在是顺推,把u节点以上的信息加上 5 dp的部分不是很多,两个DFS函数想了很久,还是没完全理解:( 6 */ 7 #inclu