Test instructions: Give a string, make a palindrome (empty is also a palindrome) where additions and deletions need a price. Q: Does it cost the least?
Idea: To change the string s to empty dp[i][j] represents the minimum cost of becoming a palindrome
For (i=m-1;i>=0;--i)
for (j=i+1;i<w;j++) LSDJFL
dp[i][j]=min (dp[i+1][j]+cost[s[i]-' A ']],dp[i][j-1]+cost[s[j]-' a ')
if (S[i]==s[j]) dp[i][j]=min (Dp[i][j],dp[i+1][j-1])
where cost represents the smallest price of additions and deletions
Code to solve the problem:
#include <iostream>#include<string>#include<algorithm>#include<cstdio>using namespacestd;intdp[2048][2048];intcost[ -];intMain () {intN, M; CIN>> N >>m; strings; CIN>>s; for(inti =0; I < n; i++) { intAdd, Del; CharC; CIN>> C >> Add >>del; Cost[c-'a'] =min (Add, Del); } for(inti=m-1; i>=0;--i) for(intj = i +1; J < M; J + +) {Dp[i][j]= min (dp[i +1][J] + cost[s[i]-'a'], Dp[i][j-1] + cost[s[j]-'a']); if(S[i] = =S[j]) dp[i][j]= Min (Dp[i][j], dp[i +1][j-1]); } printf ("%d\n", dp[0][m-1]); return 0;}
POJ 3,280 callback string problem DP algorithm