【题解】CF1107E:Vasya and Binary String

原题传送门
d p i , j , k dp_{i,j,k} dpi,j,k表示区间 [ i , j ] , j [i,j],j [i,j],j的右边还可以一起删 k k k

  • d p i , j , k = m a x ( d p i , j − 1 , 0 + a k + 1 ) dp_{i,j,k}=max(dp_{i,j-1,0}+a_{k+1}) dpi,j,k=max(dpi,j1,0+ak+1)
  • d p i , j , t = m a x ( d p k + 1 , j − 1 , 0 + d p i , k , t + 1 ( s k = s j ) ) dp_{i,j,t}=max(dp_{k+1,j-1,0}+dp_{i,k,t+1}(s_k=s_j)) dpi,j,t=max(dpk+1,j1,0+dpi,k,t+1(sk=sj))

O ( n 3 ) O(n^3) O(n3)个状态,每个状态差不多跑 O ( n ) O(n) O(n)
Code:

#include 
#define maxn 110
#define LL long long
using namespace std;
char s[maxn];
int n;
LL a[maxn], dp[maxn][maxn][maxn], sum[maxn];

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

int main(){
	n = read();
	scanf("%s", s + 1);
	for (int i = 1; i <= n; ++i){
		a[i] = read();
		for (int j = 1; j < i; ++j) a[i] = max(a[i], a[j] + a[i - j]);
	}
	for (int i = 1; i <= n; ++i)
		for (int j = i + 1; j <= n; ++j) sum[i] += (s[i] == s[j]);
	for (int i = n; i; --i)
		for (int j = i; j <= n; ++j){
			for (int k = 0; k <= sum[j]; ++k) dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][0] + a[k + 1]);
			for (int k = i; k < j; ++k) if (s[k] == s[j])
				for (int t = 0; t <= sum[j]; ++t) dp[i][j][t] = max(dp[i][j][t], dp[k + 1][j - 1][0] + dp[i][k][t + 1]);
		}
	printf("%lld\n", dp[1][n][0]);
	return 0;
}

你可能感兴趣的:(题解,codeforces,DP,题解,codeforces,DP)