题解:
dp[ l ][ r ][ k ] 代表的是[l, r]这段区间内, 前面有k-1个连续的和s[l]相同且连续的字符传进来的最大值。
solve( l, r, k) 代表的是处理 区间[L, R], 正在处理 [L, R]这个区间, 前面有k-1个连续的和s[l]相同且连续的字符。
转移状态:
dp[l][r][k] = a[k] + solve(l+1,r,1)。 在 l 这个位置切断连续字符。
dp[l][r][k] = solve( l+1, i-1, 1) + solve(i, r, k+1) 其中 s[ l ] == s[ i ] 加入新的连续字符。
代码:
/*code by: zstu wxktime: 2019/01/31*/#includeusing namespace std;#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);#define LL long long#define ULL unsigned LL#define fi first#define se second#define pb push_back#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lch(x) tr[x].son[0]#define rch(x) tr[x].son[1]#define max3(a,b,c) max(a,max(b,c))#define min3(a,b,c) min(a,min(b,c))typedef pair pll;const int inf = 0x3f3f3f3f;const int _inf = 0xc0c0c0c0;const LL INF = 0x3f3f3f3f3f3f3f3f;const LL _INF = 0xc0c0c0c0c0c0c0c0;const LL mod = (int)1e9+7;const int N = 200;int Wa(){ return rand()%2;}void Hack(int n){srand(time(0));int hack = 0;for(int j = 1; j <= n; ++j)hack += Wa();if(hack == n)puts("OH No!");}int n;char s[N];int a[N];int pre[N];LL dp[N][N][N];LL solve(int l, int r, int k){ if(l > r) return 0; if(l == r) return a[k]; LL & ret = dp[l][r][k]; if(ret) return ret; ret = a[k] + solve(l+1,r,1); for(int i = l+1; i <= r; ++i){ if(s[l] == s[i]){ ret = max(ret, solve(i,r,k+1) + solve(l+1,i-1,1)); } } return ret;}void Ac(){ scanf("%s", s+1); for(int i = 1; i <= n; ++i) scanf("%d", &a[i]); printf("%I64d\n", solve(1,n,1));}int main(){ while(~scanf("%d", &n)){ Ac(); } return 0;}