题意
有一个长度为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;
}