Exploring Pyramids UVALive - 3516 题解

2 篇文章 0 订阅

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


一棵多叉树,以左子树到右子树的顺序递归,到尽头就返回然后进入另一个分支,沿途记录下来每一个结点的字母,就可以形成一串字符串,现给你一串字符串,问你一个构造出多少个满足的多叉树。
每一步都会记录,不管是递归深搜的过程还是回溯的过程,所以从根节点出发最后回到根节点形成的字符串头和尾肯定也相同,对于任意一颗子树的遍历形成的字符串字串也需要满足这个条件,我们用分支的方法,根节点为str[0],我们枚举长度k的字段为分叉,则str[1]到str[k]构成了一个分支,那么计算这个分支的方案总数再乘以剩下的分支的方案总数就可以得到结果,用dp[i][j]表示str[i]到str[j]形成一支的方案总数,递归边缘就是i==j时dp[i][j]=1,然后记忆化搜索。

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxe = 30005;
const int maxn = 10000;
const int maxk = 505;
const int mod = 1000000000;
int n,m,k;
int dp[350][350];
string str;

int func(int i, int j){
    if (i == j)return 1;
    if (str[i] != str[j])return 0;
    if (dp[i][j] >= 0)return dp[i][j];
    int ans = 0;
    for (int k = i + 2; k <= j; k++){
        if (str[i] == str[k]){
            ans = (ans + (ll)func(i + 1, k - 1)*(ll)func(k, j)) % mod;
        }
    }
    return dp[i][j] = ans;
}


int main(){
    while (cin >> str){
        memset(dp, -1, sizeof(dp));
        int len = str.length();
        printf("%d\n", func(0, len - 1));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值