• Codeforces1107E Vasya and Binary String 记忆化dp


    Codeforces1107E 记忆化dp

    E. Vasya and Binary String

    Description:

    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 (a_x) 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 le n le 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 (a_1, a_2, dots a_n) ((1 le a_i le 10^9)), where (a_i) is the number of points for erasing the substring of length (i).

    Output

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

    Sample Input:

    7
    1101001
    3 4 9 100 1 2 3

    Sample Output:

    109

    Sample Input:

    5
    10101
    3 10 15 15 15

    Sample Output:

    23

    题目链接

    题解:

    你有一个长为n的01串,每次可以消去长度为(len​)的连续相同字符,收益为(a_{len}​),求消去整个串的收益最大值

    记忆化dp

    (dp[0,1][l][r][cnt])代表把(l)(r)删除到只剩下(cnt)个0或1的最大收益,(ans[l][r])代表把(l)(r)删完的最大收益

    转移方程为(ans[l][r] = max_{cnt = 1}^{r-l+1}(a[cnt] + dp[0,1][l][r][cnt])),(dp[c][l][r][cnt] = max_{s[i] = c, i = l}^{r - 1}(ans[l][i-1] + dp[c][i+1][r][cnt-1])),cnt=1时特判一下

    状态数为(O(n^3)),转移为(O(n)),总复杂度为(O(n^4))

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 102;
    long long dp[2][N][N][N], ans[N][N];
    int n, a[N];
    char s[N]; 
    
    long long calcdp(int c, int l, int r, int cnt);
    
    long long calcans(int l, int r) {
    	if(l > r) return 0;
    	long long &res = ans[l][r];
    	if(res != -1) return res;
    	res = 0;
    	for(int cnt = 1; cnt <= r - l + 1; ++cnt) {
    		res = max(res, a[cnt] + calcdp(0, l, r, cnt));
    		res = max(res, a[cnt] + calcdp(1, l, r, cnt));
    	}
    	return res;
    }
    
    long long calcdp(int c, int l, int r, int cnt) {
    	if(cnt == 0) return dp[c][l][r][cnt] = calcans(l, r);
    	long long &res = dp[c][l][r][cnt];
    	if(res != -1) return res;
    	res = -1e10;
    	for(int i = l; i < r; ++i) {
    		if(s[i] - '0' == c)
    			res = max(res, calcans(l, i - 1) + calcdp(c, i + 1, r, cnt - 1));
    	}
    	if(cnt == 1 && s[r] - '0' == c)
    		res = max(res, calcans(l, r - 1));
    	return res;
    }
    
    int main() {
    	scanf("%d", &n);
    	scanf("%s", s + 1);
    	for(int i = 1; i <= n; ++i) 
    		scanf("%d", &a[i]);
    	memset(dp, -1, sizeof(dp));
    	memset(ans, -1, sizeof(ans));
    	int t, a, b, c, d;
    	printf("%lld
    ", calcans(1, n));
    	return 0;
    }
    
  • 相关阅读:
    HDU 1016 Prime Ring Problem
    CreateRemoteThread函数多參数传入用法
    Dynamics CRM2015 on-premises直接升级Dynamics CRM2016 on-premises
    cocos2d-x+lua代码热载入(Hot Swap)的研究
    DirectX 9.0c游戏开发手记之“龙书”第二版学习笔记之8: Chap10: Lighting
    js合并table单元格(拼table的时候并不知道详细几行几列)
    简单图模板 Graph
    POJ-3278-Catch That Cow-广搜(BFS)
    android用存到缓存的方法来保存ListView里的数据
    Ubuntu 14.10中连接Win10的smb共享文件
  • 原文地址:https://www.cnblogs.com/tusikalanse/p/10347726.html
Copyright © 2020-2023  润新知