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 ...
随机推荐
- poj2546Circular Area(两圆相交面积)
链接 画图推公式 这两种情况 都可用一种公式算出来 就是两圆都求出圆心角 求出扇形的面积减掉三角形面积 #include <iostream> using namespace std; # ...
- zend studio12.5破解方法
其实,很简单,找到zend studio 安装目录,G:\zend studio\plugins,把文件com.zend.verifier_12.5.1.v20150514-2003.jar替换成压缩 ...
- POJ 3260 多重背包+完全背包
前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...
- 【ufldl tutorial】Softmax Regression
今天太长姿势了,什么叫懂了也写不出代码说的不就是我吗,就那么几行代码居然叽叽歪歪写了一个小时. 首先exercise要实现的是softmax的cost function和gradient,如下图: ( ...
- hiho_1078_线段树区间修改
题目 给定一组数,要求进行若干次操作,这些操作可以分为两种类型: (1) CMD 1 beg end value 将数组中下标在[beg, end] 区间内数字都变为value (2) CMD 2 b ...
- 【转】 C++ vector用法
在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. 1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<in ...
- Linux下安装最新的Eclipse
欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju/p/4781259.html GitHub地址: https://github.com/ji ...
- 基于cfx的webservice调用
一.简单的(结合Spring) 1. 新建一个web 项目,加入cfx所需要jar 2. 编写要发布的Web Service接口和实现类所需要jar 接口类 HelloWorld : import ...
- js 返回上一页和刷新
1. Javascript 返回上一页history.go(-1), 返回两个页面: history.go(-2); 2. history.back(). 3. window.history.forw ...
- 20145218 《Java程序设计》第8周学习总结
20145218 <Java程序设计>第8周学习总结 教材学习内容总结 15.1 日志 15.1.1日志API简介 java.util.logging包提供了日志功能相关类与接口,不必额外 ...