LA 3516 Exploring Pyramids (递推)
来源:互联网 发布:var(x|y) 算法 编辑:程序博客网 时间:2024/06/06 04:59
LA 3516 Exploring Pyramids
题目大意:
给一棵多叉树,从根节点开始,每次尽量往左走,走不通则回溯,将遇到的字母顺次记录下来,得到一个序列.现在给一个序列,求有多少棵树可以与之对应.
题目分析:
定义状态dp(i,j)表示序列[i,j]可形成的树的种类数。
设序列为S,因为在回溯的过程中也要记录,所以在选择某两个位置i和j时,需保证S[i]=S[j].
转移方程如下
dp(i,j)=∑dp(i+1,k−1)∗dp(k,j)|S[i]=S[k]=S[j]
边界为dp(i,i)=1,dp(i,j)=0|S[i]!=S[j]
代码:
//递推#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const int MOD=1e9;const int maxn=300+10;char S[maxn];int d[maxn][maxn];int solve(int len){ memset(d,0,sizeof(d)); for(int i=0;i<len;i++) d[i][i]=1; for(int l=2;l<=len;l++) for(int s=0;s+l-1<len;s++) { int t=s+l-1; if(S[s]!=S[t]) continue; int& ans=d[s][t]; for(int k=s+2;k<=t;k++) if(S[s]==S[k]) ans=(ans+(ll)d[s+1][k-1]*d[k][t])%MOD; } return d[0][len-1];}int main(){ while(~scanf("%s",S)) printf("%d\n",solve(strlen(S))); return 0;}
//递归#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const int MOD=1e9;const int maxn=300+10;char S[maxn];int d[maxn][maxn];int dp(int i,int j){ if(i==j) return 1; if(S[i]!=S[j]) return 0; int& ans=d[i][j]; if(ans>=0) return ans; ans=0; for(int k=i+2;k<=j;k++) if(S[i]==S[k]) ans=(ans+(ll)dp(i+1,k-1)*(ll)dp(k,j))%MOD; return ans;}int main(){ while(~scanf("%s",S)) { memset(d,-1,sizeof(d)); printf("%d\n",dp(0,strlen(S)-1)); } return 0;}
0 0
- LA 3516 Exploring Pyramids (递推)
- UVaLive ( LA ) 3516 - Exploring Pyramids (DP 递推)
- [递推]Exploring Pyramids
- POJ 2795 (LA 3516) Exploring Pyramids 解题报告(递推)
- UVa 1362(LA 3516) Exploring Pyramids
- LA 3516 Exploring Pyramids 多叉树遍历
- LA 3516 Exploring Pyramids 记忆化搜索
- UVALive 3516 Exploring Pyramids——递推
- LA3516 Exploring Pyramids 递推关系
- 多叉树计数,Exploring Pyramids, NEERC 2005, LA 3516
- LA 3516 - Exploring Pyramids (记忆化搜索dp)
- UVALive - 3516 Exploring Pyramids
- UVALive 3516Exploring Pyramids
- UVALive 3516 Exploring Pyramids
- uva live 3516 - Exploring Pyramids
- UVA-3516 Exploring Pyramids (DP)
- uva live 3516 Exploring Pyramids 区间DP
- UVAlive - 3516 —— Exploring Pyramids
- Npoi操作excel
- OpenGL-- Shader 颜色 光照
- 【BZOJ1407】 [Noi2002]Savage
- VMwareworkstations12.0.1里创建centOS6.8虚拟机及克隆
- git远程分支和本地分支
- LA 3516 Exploring Pyramids (递推)
- Spring事务处理探究
- 【Web】css强制换行和超出隐藏实现...
- 实现两个Mysql数据库之间同步的方案
- Java注解基础概念总结
- adapter 构造器中的 一个item中再增加 item的时用for循环增加viewde问题
- Android -饼状图简单使用
- java实现从头部及尾部删除指定长度字符
- OpenGL ES之glUniform函数-----非常清晰的解释了glUniform的函数使用, 很不错!!!