https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1517

久违的树形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的更多相关文章

  1. LA 3516 Exploring Pyramids (记忆化搜索)

    题意 给定一个DFS序列,问能有多少树与之对应. 思路 设输入序列为S,dp(i, j)为子序列Si, Si+1, --, Sj对应的树的个数,则边界条件为d(i, i) = 1,且Si != Sj时 ...

  2. 【区间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 ...

  3. LA 3516(ZOJ 2641) Exploring Pyramids(递推 DP)

    Exploring Pyramids Archaeologists have discovered a new set of hidden caves in one of the Egyptian p ...

  4. LA3516 Exploring Pyramids

    Exploring Pyramids 题目大意:给定一个欧拉序列(即每经过一个点,把这个点加入序列),问有多少种对应的多叉树 序列与树构造对应问题,考虑区间DP dp[i][j]表示序列i...j对应 ...

  5. LA 3516 (计数 DP) Exploring Pyramids

    设d(i, j)为连续子序列[i, j]构成数的个数,因为遍历从根节点出发最终要回溯到根节点,所以边界情况是:d(i, i) = 1; 如果s[i] != s[j], d(i, j) = 0 假设第一 ...

  6. Exploring Pyramids UVALive - 3516 (记忆化DP)

    题意:给定一个序列 问有多少棵树与之对应 题目连接:https://cn.vjudge.net/problem/UVALive-3516 对于这一序列  分两种2情况 当前分支 和 其它分支  用df ...

  7. UVA 1362 Exploring Pyramids 区间DP

    Archaeologists have discovered a new set of hidden caves in one of the Egyptian pyramids. The decryp ...

  8. Gym 101334E Exploring Pyramids(dp+乘法原理)

    http://codeforces.com/gym/101334 题意: 给出一棵多叉树,每个结点的任意两个子节点都有左右之分.从根结点开始,每次尽量往左走,走不通了就回溯,把遇到的字母顺次记录下来, ...

  9. 101334E Exploring Pyramids

    传送门 题目大意 看样例,懂题意 分析 实际就是个区间dp,我开始居然不会...详见代码(代码用的记忆化搜索) 代码 #include<iostream> #include<cstd ...

随机推荐

  1. nodejs学习笔记<一>安装及环境搭建

    零零散散学了几天nodejs,进度一直停滞不前,今天沉下心来好好看了下nodejs的介绍和代码.自己也试着玩了下,算是有点入门了. 这里来做个学习笔记. ——————————————————————— ...

  2. PHP SPL标准库之SplFixedArray使用实例

    SplFixedArray主要是处理数组相关的主要功能,与普通php array不同的是,它是固定长度的,且以数字为键名的数组,优势就是比普通的数组处理更快. 看看我本机的Benchmark测试: i ...

  3. netbeans for php常用快捷键

    文本编辑 复制当前行到上一行/下一行ctl+shf+up 移动当前行到上一行/下一行shf+alt+left/right/down/up 删除当前行ctl+E 生成下一行shf+enter(回车) 大 ...

  4. unsigned char 无符号整形 减法运算

    对于一个字节来说: unsigned char :     0  ~  255              0000 0000  ~ 1111 1111 char :-128  ~  127       ...

  5. ajax中向HTML页面中指定位置添加信息

    $.ajax({  type : "POST",  beforeSend : function() {   showLoader("数据加载中...");// ...

  6. java 集合(set)

    Interface ListIterator<E> 特有的方法: hasPrevious() 判断是否存在上一个元素. previous() 当前指针先向上移动一个单位,然后再取出当前指针 ...

  7. linux笔记:压缩解压命令gzip,gunzip,tar,zip,unzip,bzip2,bunzip2

    命令名称:gzip功能:压缩文件命令所在路径:/bin/gzip用法:gzip 文件压缩后文件格式:.gz其他:压缩后不保留原文件:只能压缩文件,不能压缩目录 命令名称:gunzip功能:解压.gz格 ...

  8. 20145218 《Java程序设计》第9周学习总结

    20145218 <Java程序设计>第9周学习总结 教材学习内容总结 16.1.1 JDBC简介 JDBC全名Java DataBase Connectivity,是java联机数据库的 ...

  9. HDUOJ------3336 Count the string(kmp)

    D - Count the string Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  10. 79. 212. Word Search *HARD* -- 字符矩阵中查找单词

    79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...