UVa 1362 Exploring Pyramids:多叉树遍历&DP

1362 - Exploring Pyramids

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=469&page=show_problem&problem=4108

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 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.

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

dp(i,j)=sum{dp(i+1,k-1)*dp(k,j) | i+2<=k<=j,Si=Sk+Sj}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索output
, 多叉树
, robot
, of
, The
, have
, Pyramid
, 安装pyramid
that
exploring、never stop exploring、exploring strategy、celestial exploring、exploring es6 pdf,以便于您获取更多的相关知识。

时间: 2024-12-20 17:01:08

UVa 1362 Exploring Pyramids:多叉树遍历&amp;DP的相关文章

uva live 3516 - Exploring Pyramids

点击打开链接 题意:给出一棵多叉树,每个结点的任意两个子节点都有左右之分.从根节点开始,每次尽量往左走,走不通就回溯,把遇到的字母顺序记录下来,可以得到一个序列.给定一个序列,问有几种满足的多叉树. 思路: 1 设输入的序列为S,dp[i][j]为子序列Si,Si+1...Sj对应的树的个数,则边界条件是dp[i][i] = 1,且Si不等于Sj时dp[i][j] = 0. 2 这样在非边界情况下,Si = Sj递推的关系为dp[i][j] = sum{dp[i+1][k-1]*dp[k][j]

UVa 11234 Expressions:二叉树 层次遍历 广搜

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=2175 题目类型: 数据结构, 二叉树 题目大意: 一般情况下,都是中缀操作符, 如x+y.然后题目给出了一种后缀操作符的形式, 变成 x y +. 进行后缀操作可以用栈模拟,使用push,pop, 过程和经典的"括号匹配"差不

UVa 548 Tree:中序遍历&amp;amp;后序遍历&amp;amp;DFS

548 - Tree Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489 You are to determine the value of the leaf node in a given binary tree that is the termina

ascii-二叉树遍历全都是出ASCII码

问题描述 二叉树遍历全都是出ASCII码 #include <iostream> using namespace std; const int Maxsize = 100; struct Node { int data; Node*lchild,*rchild; }; class Tree { public: Tree(){cout << "输入节点信息,如果是空请输入"#"" << endl; root = Creat(root

java中二叉树遍历(递归) 程序代码

测试二叉树遍历,递归算法  代码如下 复制代码       public class TestBinaryTree {     public static void main(String[] args) {     Node<String> g = new Node<String>("G", null, null):     Node<String> e = new Node<String>("E", null, n

uva 10688:The Poor Giant(区间dp)

题目链接: uva-10688 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=514&page=show_problem&problem=1629 题意 有n个苹果,和一个数k,第i个苹果的重量是k+i(1<=i<=n). 已知其中只有一个苹果是甜的, 所有比它重量轻的都是苦的,比它重的都是酸的. 为了要找出甜的苹果,就要去一个一个地吃它,且吃了咬了苹果

UVa 10616 Divisible Group Sums:DFS以及DP

10616 - Divisible Group Sums Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1557 思路:用DFS+记忆化搜索枚举组合,注意数字有<0的. return dp[i][cnt][sum] = f(i + 1, cnt +

算法:uva 1407 Caves (树形背包dp)

题意 一棵n个节点的树,树的边有正整数权,表示两个节点之间的距离.你的任务是回答这样的询问:从跟 节点出发,走不超过x单位的距离, 最多能经过多少节点?同一个节点经过多次, 只能算一个. 思路 这题同样是多天前看的, 在今天才想出解法的. 动态规划就是这么有意思 :) 遍历n个节点, 有两种情 况, 第一种是遍历完之后不回到出发点, 第二种是要回到出发点. 两种都可能会重复经过某些边, 但是显 然还是第二种遍历的花费会更大 在这一题中, 遍历之后不需要回到出发点. f(i, j, 0): 表示遍

Android应用性能优化最佳实践.2.3 布局优化

2.3 布局优化 布局是否合理主要影响的是页面测量时间的多少,我们知道一个页面的显示测量和绘制过程都是通过递归来完成的,多叉树遍历的时间与树的高度h相关,其时间复杂度为O(h),如果层级太深,每增加一层则会增加更多的页面显示时间. 任何时候View中的绘制内容发生变化时,都需要重新创建DisplayList.渲染DisplayList,更新到屏幕上等一系列操作.这个流程的表现性能取决于View的复杂程度.View的状态变化以及渲染管道的执行性能.例如,假设某个Button的大小需要增大到目前的两