DP 记忆搜索 E. Vasya and Binary String

题意

有一个长度为n的01串
你可以删除连续的’0’或’1’并获得相应的ai
直到把01串删除完毕
最多可以获得多少价值

思路

记忆DP
dp[l][r][len]
l 表示起点
r 表示终点
len 表示长度
dp[i][r][len]就是在i到r区间内删除
接下来看来代码注释

AC代码

#include <bits/stdc++.h>
#define endl "\n" 
#define INF 0x3f3f3f3f3f3f3f3f
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
using namespace std;
typedef long long ll;
const  ll mod = 1e9 + 7;
const   double  PI = acos(-1.0);
const double EI = exp(1.0);
const int  N = 250;
const int maxn = 205;
const double  eps = 1e-8;
int icase = 0;
ll a[N];
ll dp[N][N][N];
string str;
ll dfs(int l, int r, int len)
{
	if (l > r)return 0;
	if (l == r)return a[len];
	if (dp[l][r][len])return dp[l][r][len];//记忆化防止重复
	ll ans = a[len] + dfs(l + 1, r, 1);//最初的ans是从当前l到r所有全部只一个个删除的值
	for (int i = l + 1; i <= r; i++)
	{
		if (str[l-1] == str[i-1])
			ans = max(ans, dfs(l + 1, i - 1, 1) + dfs(i, r, len + 1));//这个转移 是考虑第i和第j相同 并将其连起来删除 l+1到i-1还是一个个的删掉  然后长度为 len+1的++  其他的还是一个个
	}
	return dp[l][r][len]=ans;
}
void solve()
{
	int n;
	cin >> n >> str;
	for (int i = 1; i <= n; i++)
		cin>>a[i];
	cout << dfs(1, n, 1) << endl;

}
int main()
{
	std::ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	//int t; cin >> t; while (t--)
		solve();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值