DP[I][J] Indicates the cost of processing I to J, if s[i] = = S[j] Do not need to process, otherwise processing s[i] or s[j],
For a character ch, plus ch or delete ch, the effect is the same for interval transfer, taking min.
#include <cstdio>#include<iostream>#include<string>#include<cstring>#include<queue>#include<vector>#include<stack>#include<vector>#include<map>#include<Set>#include<algorithm>using namespacestd;Const intSigma_size = -, MAXM = 2e3+5;CharS[MAXM];intAdd[sigma_size], del[sigma_size];intCost[sigma_size];intDP[MAXM][MAXM];//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif intN, M; scanf ("%d%d",&n,&m); scanf ("%s", s); for(inti = n; i--;){ Charch[2]; scanf ("%s", CH); intid = *ch-'a'; scanf ("%d%d", add+id,del+ID); Cost[id]=min (Add[id],del[id]); } for(intL =1; L < M; l++){ for(inti =0; I+l < M; i++){ intj = i+m; if(S[i] = =S[j]) {Dp[i][j]= dp[i+1][j-1];//there may be i+1 = j, j-1 = i, but initially 0 so there is no effect}Else{Dp[i][j]= Min (dp[i+1][J] + cost[s[i]-'a'], dp[i][j-1] + cost[s[j]-'a']) ; }}} printf ("%d\n", dp[0][m-1]); return 0;}
POJ 3280 Cheapest Palindrome