CF - 1107 E Vasya and Binary String DP

題目傳送門c++

題解:ide

  dp[ l ][ r ][ k ] 表明的是[l, r]這段區間內, 前面有k-1個連續的和s[l]相同且連續的字符傳進來的最大值。spa

  solve( l, r, k) 表明的是處理 區間[L, R],  正在處理 [L, R]這個區間, 前面有k-1個連續的和s[l]相同且連續的字符。code

轉移狀態:blog

  dp[l][r][k] = a[k] + solve(l+1,r,1)。 在 l 這個位置切斷連續字符。get

  dp[l][r][k] = solve( l+1, i-1, 1) + solve(i, r, k+1)  其中 s[ l ] == s[ i ]  加入新的連續字符。it

 

代碼:class

/* code by: zstu wxk time: 2019/01/31 */ #include<bits/stdc++.h>
using 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<int,int> 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; }
View Code
相關文章
相關標籤/搜索