給定長度
轉移:
dp[i][0] = max(dp[i-1][0], dp[i-1][1] + a[i]);
dp[i][1] = max(dp[i-1][1], dp[i-1][0] + a[i]);
答案為
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MOD = 1e9+7;
const int MAXN = 200005;
char str[MAXN];
int a[MAXN], dp[MAXN][2];
int main() {
int T; scanf("%d", &T);
for (int t = 1; t <= T; t++) {
scanf("%s", str);
int n = strlen(str);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
dp[0][0] = dp[0][1] = 0;
if (str[0] != '1') dp[0][0] = 0;
if (str[0] != '0') dp[0][1] = 0;
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i][1] = -1e9;
if (str[i] != '1') {
dp[i][0] = max(dp[i-1][0], dp[i-1][1] + a[i]);
}
if (str[i] != '0') {
dp[i][1] = max(dp[i-1][1], dp[i-1][0] + a[i]);
}
}
int ans = max(dp[n-1][0], dp[n-1][1]);
printf("Case #%d: %d\n", t, ans);
}
return 0;
}