LA 3516 - Exploring Pyramids
久违的树形dp
dp[l][r] 表示在l到r之间字符串形成的子树有多少种
然后要枚举最左树枝所到位置 假设是 i 那么从l+1到i-1 递归就是最左树枝的种类 然后乘上剩下的部分
剩下的部分i到r相当是去掉了最左树枝的又一个子树,递归就可以
代码:
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <queue>
- #define ll long long
- using namespace std;
- const ll MOD= 1000000000;
- const int N=305;
- ll dp[N][N];
- char s[N];
- ll dfs(int l,int r)
- {
- if(dp[l][r]!=-1)
- return dp[l][r];
- if(l>r)
- return (dp[l][r]=0);
- if(l==r)
- return (dp[l][r]=1);
- if(s[l]!=s[r])
- return (dp[l][r]=0);
- dp[l][r]=0;
- for(int i=l+1;i<=r;++i)
- if(s[i]==s[l])
- dp[l][r]=(dp[l][r]+dfs(l+1,i-1)*dfs(i,r))%MOD;
- return dp[l][r];
- }
- int main()
- {
- //freopen("data.in","r",stdin);
- while(scanf("%s",s)!=EOF)
- {
- int n=strlen(s);
- memset(dp,-1,sizeof(dp));
- cout<<dfs(0,n-1)<<endl;
- }
- return 0;
- }
LA 3516 - Exploring Pyramids的更多相关文章
- LA 3516 Exploring Pyramids (记忆化搜索)
题意 给定一个DFS序列,问能有多少树与之对应. 思路 设输入序列为S,dp(i, j)为子序列Si, Si+1, --, Sj对应的树的个数,则边界条件为d(i, i) = 1,且Si != Sj时 ...
- 【区间dp】【记忆化搜索】UVALive - 3516 - Exploring Pyramids
f(i,j)=sum(f(i+1,k-1)*f(k,j) | i+2<=k<=j,Si=Sk=Sj). f(i+1,k-1)是划分出第一颗子树,f(k,j)是划分出剩下的子树. #incl ...
- LA 3516(ZOJ 2641) Exploring Pyramids(递推 DP)
Exploring Pyramids Archaeologists have discovered a new set of hidden caves in one of the Egyptian p ...
- LA3516 Exploring Pyramids
Exploring Pyramids 题目大意:给定一个欧拉序列(即每经过一个点,把这个点加入序列),问有多少种对应的多叉树 序列与树构造对应问题,考虑区间DP dp[i][j]表示序列i...j对应 ...
- LA 3516 (计数 DP) Exploring Pyramids
设d(i, j)为连续子序列[i, j]构成数的个数,因为遍历从根节点出发最终要回溯到根节点,所以边界情况是:d(i, i) = 1; 如果s[i] != s[j], d(i, j) = 0 假设第一 ...
- Exploring Pyramids UVALive - 3516 (记忆化DP)
题意:给定一个序列 问有多少棵树与之对应 题目连接:https://cn.vjudge.net/problem/UVALive-3516 对于这一序列 分两种2情况 当前分支 和 其它分支 用df ...
- UVA 1362 Exploring Pyramids 区间DP
Archaeologists have discovered a new set of hidden caves in one of the Egyptian pyramids. The decryp ...
- Gym 101334E Exploring Pyramids(dp+乘法原理)
http://codeforces.com/gym/101334 题意: 给出一棵多叉树,每个结点的任意两个子节点都有左右之分.从根结点开始,每次尽量往左走,走不通了就回溯,把遇到的字母顺次记录下来, ...
- 101334E Exploring Pyramids
传送门 题目大意 看样例,懂题意 分析 实际就是个区间dp,我开始居然不会...详见代码(代码用的记忆化搜索) 代码 #include<iostream> #include<cstd ...
随机推荐
- Python学习(1)安装Python
***** 安装Python 在官网上 https://www.python.org/downloads/ 可以看到有3.5.1与2.7.11两个版本,我这里用的是3.5.1版本 我用的是win7/ ...
- 【转载】Linux系统,设置Oracle开机启动,待整理
http://www.cnblogs.com/mophee/archive/2013/06/03/3115805.html
- Python 调用自定义包
创建包 # mkdir -p /python/utils # touch /python/utils/__init__.py # vi /python/utils/Log.pyimport timed ...
- 基于gralde搭建spring boot项目
搭建基于gradle的sprint boot项目,swagger-ui辅助 spring boot官网:http://projects.spring.io/spring-boot/get start ...
- OpenCV3编程入门笔记(2)计时函数、感兴趣区域RIO、分离/混合通道
11 绘制直线的line函数 DrawLine(Mat img, Pont start, Point end); 绘制椭圆的ellipse函数 DrawEllipse(Mat img, dou ...
- semantic-ui dropdown is not a function
按照semantic-ui官网示例,编写了如下示例,却不见效果. <div class="ui secondary menu"> <a class="i ...
- phpMyAdmin中sql-parser组件的使用
版权声明:本文由陈苗原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/206 来源:腾云阁 https://www.qclou ...
- eclipse 新建 maven 项目 添加 spring hibernate 的配置文件 详情
主要配置文件 pom.xml 项目的maven 配置文件 管理项目所需 jar 依赖支持 web.xml 项目的总 配置文件 :添加 spring和hibernate 支持 applicationC ...
- form表单控件
$("select option[value='" + queryparams['vendor'] + "']").attr("selected&qu ...
- 理论与实践中的 C# 内存模型
转载自:https://msdn.microsoft.com/magazine/jj863136 这是该系列(包含两部分内容)的第一部分,这部分将以较长的篇幅介绍 C# 内存模型. 第一部分说明 C# ...