Exploring Pyramids

Archaeologists have discovered a new set of hidden caves in one of the Egyptian pyramids. The decryption of ancient hieroglyphs on the walls nearby showed that the caves structure is as follows. There are n <tex2html_verbatim_mark>caves in a pyramid, connected by narrow passages, one of the caves is connected by a passage to the outer world. The system of the passages is organized in such a way, that there is exactly one way to get from outside to each cave along passages. All caves are located in the basement of the pyramid, so we can consider them being located in the same plane. Passages do not intersect. Each cave has its walls colored in one of several various colors.

The scientists have decided to create a more detailed description of the caves, so they decided to use an exploring robot. The robot they are planning to use has two types of memory - the output tape, which is used for writing down the description of the caves, and the operating memory organized as a stack.

The robot first enters the cave connected to the outer world along the passage. When it travels along any passage for the first time, it puts its description on the top of its stack. When the robot enters any cave, it prints the color of its walls to its output tape. After that it chooses the leftmost passage among those that it has not yet travelled and goes along it. If there is no such passage, the robot takes the passage description from the top of its stack and travels along it in the reverse direction. The robot's task is over when it returns to the outside of the pyramid. It is easy to see that during its trip the robot visits each cave at least once and travels along each passage exactly once in each direction.

The scientists have sent the robot to its mission. After it returned they started to study the output tape. What a great disappointment they have had after they have understood that the output tape does not describe the cave system uniquely. Now they have a new problem - they want to know how many different cave systems could have produced the output tape they have. Help them to find that out.

Since the requested number can be quite large, you should output it modulo 1 000 000 000. Please note, that the absolute locations of the caves are not important, but their relative locations are important, so the caves (c) and (d) on the picture below are considered different.

<tex2html_verbatim_mark>

Input

The input file contains several test cases, and each of them consists of a single line with the output tape that the archaeologists have. The output tape is the sequence of colors of caves in order the robot visited them. The colors are denoted by capital letters of the English alphabet. The length of the tape does not exceed 300 characters.

Output

For each input case, write to the output a single line containing one integer number - the number of different cave systems (modulo 1 000 000 000) that could produce the output tape.

Sample Input

ABABABA
AB

Sample Output

5
0 题目大意:给出一棵多叉树,每个结点的任意两个子结点都有左右之分。从根结点开始,每次尽量往左走,走不通了就回溯,把遇到的字母顺序记录下来,可以得到一个序列。如图所示的序列均为 ABABABA 。给定一个序列,问有多少棵树与之对应。 分析:设输入序列为S,d(i,j)为子序列Si,Si+1,...,Sj对应的个数,则边界条件是d(i,i)=1,且Si不等于Sj时d(i,j)=0(因为起点和终点应是同一点)。在其他情况下,设第一个分支在Sk时回到树根(必须有Si=Sk),则这个分支对应的序列是Si+1,...,Sk-1,方案数为d(i+1,k-1),其他分支对应的访问序列为Sk,...,Sj,方案数为d(k,j)。这样,在非边界情况,递推关系为d(i,j)=sum{d(i+1,k-1)*d(k,j)|i+2<=k<=j,Si=Sk=Sj} 代码如下
#include<cstdio>
#include<cstring>
using namespace std; const int maxn = + ;
const int MOD = ;
typedef long long LL; char S[maxn];
int d[maxn][maxn]; int dp(int i, int j) {
if(i == j) return ;
if(S[i] != S[j]) return ;
int& ans = d[i][j];
if(ans >= ) return ans;
ans = ;
for(int k = i+; k <= j; k++) if(S[i] == S[k])
ans = (ans + (LL)dp(i+,k-) * (LL)dp(k,j)) % MOD;
return ans;
} int main() {
while(scanf("%s", S) == ) {
memset(d, -, sizeof(d));
printf("%d\n", dp(, strlen(S)-));
}
return ;
}
 

LA 3516(ZOJ 2641) Exploring Pyramids(递推 DP)的更多相关文章

  1. 递推DP URAL 1167 Bicolored Horses

    题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...

  2. 递推DP URAL 1017 Staircases

    题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...

  3. 递推DP URAL 1260 Nudnik Photographer

    题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...

  4. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  5. 递推DP URAL 1119 Metro

    题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...

  6. 递推DP 赛码 1005 Game

    题目传送门 /* 递推DP:官方题解 令Fi,j代表剩下i个人时,若BrotherK的位置是1,那么位置为j的人是否可能获胜 转移的时候可以枚举当前轮指定的数是什么,那么就可以计算出当前位置j的人在剩 ...

  7. 递推DP HDOJ 5328 Problem Killer

    题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...

  8. hdu1978(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1978 分析: 递推DP. dp[][]表示可以到达改点的方法数. 刚开始:外循环扫描所有点dp[x][ ...

  9. 递推DP URAL 1031 Railway Tickets

    题目传送门 /* 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 注意:s1与s2大小不一定,坑! 详细解释:http://blog.csdn.net/kk303/article/d ...

随机推荐

  1. vim配置vimrc详解

    vimrc的存放位置: 系统 vimrc 文件: "$VIM/vimrc" 用户 vimrc 文件: "$HOME/.vimrc" 用户 exrc 文件: &q ...

  2. Polya定理

    http://www.cnblogs.com/wenruo/p/5304698.html 先看 Polya定理,Burnside引理回忆一下基础知识.总结的很棒. 一个置换就是集合到自身的一个双射,置 ...

  3. storm核心组件

    Storm核心组件 了解 Storm 的核心组件对于理解 Storm 原理非常重要,下面介绍 Storm 的整体,然后介绍 Storm 的核心. Storm 集群由一个主节点和多个工作节点组成.主节点 ...

  4. nyoj 540 奇怪的排序

    奇怪的排序 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 最近,Dr. Kong 新设计一个机器人Bill.这台机器人很聪明,会做许多事情.惟独对自然数的理解与人类 ...

  5. 数值类对象:NSNumber,NSValue,NSNull

    基本,集合,复杂,对象 可用对象封装基本数值,然后将对象放入NSArray或NSDictionary 中. 用对象封装基本数值后,即可给其发送消息. 数值类型包括:NSNumber,NSValue,N ...

  6. 8-14-Exercise(博弈:HDU 1846 & HDU 1527 )

    B.HDU 1846    Brave Game 算是最简单的入门博弈题吧...... 呃......我用的......算是不是方法的方法吧——找规律~ 可以发现:X-M为奇数时,先手会输:而为偶数的 ...

  7. Java比较运算符

    注意哦: 1.  > . < . >= . <= 只支持左右两边操作数是数值类型 2.  == . != 两边的操作数既可以是数值类型,也可以是引用类型 public clas ...

  8. 【转】Java中Vector和ArrayList的区别

    首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...

  9. 【移动开发】怎样自己定义ViewGroup

    本文翻译自<50 android hacks> 按照惯例.先从一个样例说起. 非常easy,3张扑克牌叠在一起显示. 这个布局效果该怎样实现呢?有的同学该说了,这非常easy啊,用Rela ...

  10. PL/SQL Developer 在windows7 64位系统下连Oaracle11g64位系统的解决经验

    PL/SQL Developer 在windows7 64位系统下连Oaracle11g64位系统的解决经验 一.问题现象及解决方法 现象: 1.PL/SQL 无法登录64位数据库 2.在PL/SQL ...