Codeforces Contest 1107 problem E Vasya and Binary String —— 记忆化搜索

Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of length n.

Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets ax points for erasing substring of length x.

Vasya wants to maximize his total points, so help him with this!

Input
The first line contains one integer n (1≤n≤100) — the length of string s.

The second line contains string s, consisting only of digits 0 and 1.

The third line contains n integers a1,a2,…an (1≤ai≤109), where ai is the number of points for erasing the substring of length i.

Output
Print one integer — the maximum total points Vasya can get.

Examples
inputCopy
7
1101001
3 4 9 100 1 2 3
outputCopy
109
inputCopy
5
10101
3 10 15 15 15
outputCopy
23
Note
In the first example the optimal sequence of erasings is: 1101001 → 111001 → 11101 → 1111 → ∅.

In the second example the optimal sequence of erasings is: 10101 → 1001 → 11 → ∅.

题意:

给你一个串,你可以将相邻的一些相同的串删除,将剩下的串合并,并得到删除串的长度对应的值,最终删掉所有的串,问你获得的最大值是多少。

题解:

对于每一个位置,要么它与前面的相同数合并,要么与后面的合并,那么dp[l][r][sum]就表示在l及其之前的相同数删除掉的最大值,sum就表示相同数的数量
ans=max(ans,dfs(l+1,i-1,1)+dfs(i,r,sum+1));表示l和i合并所能获得的最大值。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[105],dp[105][105][105];
char s[105];
ll dfs(int l,int r,int sum)
{
    if(l>r)
        return 0;
    if(l==r)
        return a[sum];
    if(dp[l][r][sum])
        return dp[l][r][sum];
    ll ans=a[sum]+dfs(l+1,r,1);
    for(int i=l+1;i<=r;i++)
        if(s[l]==s[i])
        ans=max(ans,dfs(l+1,i-1,1)+dfs(i,r,sum+1));
    dp[l][r][sum]=ans;
    return ans;
}
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",s+1);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    printf("%lld\n",dfs(1,n,1));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/88068671