uva live 3516 Exploring Pyramids 区间DP

本文介绍了一道关于多叉树的区间动态规划问题,通过先序遍历获得的字符串来计算符合条件的多叉树数量。核心思路在于利用记忆化搜索解决重复子问题,并考虑根节点的回溯特性。
//	uva live 3516 Exploring Pyramids 区间DP
//
//	题目大意:
//
//		给你一个多叉树,每个节点是一个大写字母,从根节点走,按照先序遍历的
//	原则访问,不能访问则回溯,每次记录一下节点的字符,最后得到一个字符串.现
//	在给你一个字符串,问可能符合条件的多叉树的数量.
//
//	解题思路:
//
//		区间DP,我们注意到,从根节点出发,一定会再次回到根节点,那么我们可以设
//	d(i,j) 是序列i到j段形成的符合条件的多叉树的数量,则
//	i~j一定会有一个k使得s[i] = s[k]{因为一定要回到i}.所以问题就转化为i+1~k-1
//	的子树和k~j树中的另外一个部分.这两部分乘积的结果就是区间的答案.采用记忆化
//	搜索.用vis标记是否访问过.边界为d(i,i) = 1,s[i]!=s[j]时,d(i,j)=0,不要忘记
//	要回到根如果头和尾不相等,肯定回不去的.
//
//	感悟:
//
//		区间DP我知道,状态我想的到,但是状态的转移,不是非常清楚明白.继续加油吧~~~
//	FIGHTING!!!



#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;

typedef long long ll;
const int MAX_N = 308;
const int MOD = 1000000000;

int n;

char s[MAX_N];
ll d[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];

ll dp(int x,int y){
	if (x > y)
		return d[x][y] = 0;
	if (x == y)
		return d[x][y] = 1;
	if (s[x]!=s[y])
		return 0;
	if (vis[x][y])
		return d[x][y];

	vis[x][y] = 1;

	ll& ans = d[x][y];

	ans = 0;

	for (int k=x+2;k<=y;k++){ // 注意,这里最少走两步,走一步就是自己到自己
		if (s[x] == s[k])
			ans = (ans + dp(x+1,k-1)%MOD * dp(k,y))%MOD;
	}
	return ans % MOD;

}

void solve(){
	memset(d,0,sizeof(d));
	memset(vis,0,sizeof(vis));
	printf("%lld\n",dp(0,strlen(s)-1));
}

int main(){
	//freopen("1.txt","r",stdin);
	while(scanf("%s",s)!=EOF){
		solve();
	}
}

### UVA 12000: A Programming and Algorithm Perspective UVA 12000, commonly referred to in competitive programming circles, is a problem that involves algorithmic thinking and efficient implementation. While no direct description of UVA 12000 is provided in the references, we can infer potential strategies based on common patterns seen in similar problems like UVA 1363 and other Josephus problem variants. The problem likely involves a sequence or array manipulation, possibly with modular arithmetic or cyclic behavior. Based on the pattern of other UVA problems involving recursive or iterative logic, UVA 12000 may require a solution that leverages mathematical observations to reduce complexity. For instance, if UVA 12000 involves a similar structure to the Josephus problem, the solution could be expressed using a loop that iteratively computes the position of the survivor or some other derived value. The following code snippet demonstrates a general approach that could be adapted depending on the exact requirements of the problem: ```python def solve(n, k): r = 0 for i in range(1, n + 1): r = (r + k) % i return r ``` This function computes a result using an iterative approach where `k` is added to the current result `r` and then the modulo operation is applied with the current index `i`. This is a common technique in problems involving circular elimination or selection processes. In the case of UVA 12000, the problem may require additional constraints or a variation of this logic. For example, it could involve handling large input sizes efficiently, which would necessitate optimizing the algorithm to avoid unnecessary computations. Techniques such as memoization or dynamic programming could play a role in achieving this efficiency. When implementing a solution, it is crucial to consider edge cases, such as when `n` or `k` is very small or when `k` exceeds `n`. Handling these cases correctly ensures robustness in the implementation. For example, if `k` is larger than `i` during the loop iteration, the modulo operation naturally reduces it to an equivalent smaller value. If the problem involves prime numbers or paths, such as in UVA 12101, then a breadth-first search (BFS) or similar graph traversal technique might be required. In such cases, the solution would involve generating valid transitions between states and efficiently exploring the search space. ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值