Uvalive 3516 - Exploring Pyramids(DP)

uvalive 3516

Archaeologists have discovered a new set ofhidden caves in one of the Egyptian pyramids. The decryption of ancienthieroglyphs on the walls nearby showed that the caves structure is as follows. Thereare n caves in a pyramid, connected by narrow passages, one of the caves isconnected by a passage to the outer world. The system of the passages isorganized in such a way, that there is exactly one way to get from outside toeach 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 notintersect. 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 usehas two types of memory — the output tape, which is used for writing down thedescription of the caves, and the operating memory organized as a stack. Therobot 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 descriptionon the top of its stack. When the robot enters any cave, it prints the color ofits walls to its output tape. After that it chooses the leftmost passage amongthose that it has not yet travelled and goes along it. If there is no suchpassage, the robot takes the passage description from the top of its stack andtravels along it in the reverse direction. The robot’s task is over when itreturns to the outside of the pyramid. It is easy to see that during its tripthe robot visits each cave at least once and travels along each passage exactlyonce in each direction. The scientists have sent the robot to its mission.After it returned they started to study the output tape. What a greatdisappointment they have had after they have understood that the output tapedoes not describe the cave system uniquely. Now they have a new problem — theywant to know how many different cave systems could have produced the outputtape they have. Help them to find that out. Since the requested number can bequite large, you should output it modulo 1 000 000 000. Please note, that theabsolute locations of the caves are not important, but their relative locationsare important, so the caves (c) and (d) on the picture below are considereddifferent.

 

Input

The input file contains the output tapethat the archaeologists have. The output tape is the sequence of colors ofcaves in order the robot visited them. The colors are denoted by capitalletters of the English alphabet. The length of the tape does not exceed 300characters.

 

Output

Output one integer number — the number ofdifferent cave systems (modulo 1 000 000 000) that could produce the outputtape.

 

Sample Input

ABABABA

AB

Sample Output

5 0

 

【题意】

给定一个全为大写字母的序列,(一个大写字母代表树的一个结点),求出有多少颗不同的树满足前序遍历的序列恰好为所给的序列?

 

【思路】

这是一道经典的动态规划问题。假设dp(i,j)表示序列中从i到j的子序列对应的树的数量。因为树的遍历一定是从根结点开始最后回到根结点,所以当s[i]!=s[j]时,dp(i,j)=0,当i==j时,只有一个结点,也就只有一种遍历方法,所以dp(i, i)=1。对于一般情况,因为这棵树可能是多叉树,所以我们考虑从整个序列中先选出一个分叉,那么从根结点i开始遍历,当遍历完一个分叉后一定会回到根,也就是当s[k+1]==s[i]时那么s[i+1]-s[k]就可以看成是从根结点开始的一个分叉,这个分叉确立后对应的解即为dp(i+1,k)*dp(k+1,j),s[i]==s[k+1]==s[j]。所以总的递推式就是dp(i,j)=sum{dp(i+1,k)*dp(k+1,j),i<k<j},用记忆化搜索求解。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 350;
const int mod = 1e9;

int n;
char s[maxn];
ll dp[maxn][maxn]; 

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

int main() {
	while (scanf("%s", s) == 1) {
		n = strlen(s);
		memset(dp, -1, sizeof(dp));
		int ans = dfs(0, n - 1);
		printf("%d\n", ans);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值