"The main topic"
Gives a string that can delete or add some characters, each of which consumes value. Ask at least how much value, can make a string into a palindrome.
Ideas
In fact the value of deleting or adding characters only needs to keep the smaller one. Assuming that you are currently converting (j,i) to a literal, it has the following three scenarios:
(1) Add or remove at the end of a character that is the same as the beginning, f[j][i-1]+cost[s[i]-' a ';
(2) Adding or deleting a character at the beginning and ending the same, f[j+1][i]+cost[s[j]-' a ';
(3) If the beginning and end of the character is the same, there is f[j+1][i-1].
Error
Initialization is 0, do not set the INF.
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intmaxn= -+ -;7 intn,m;8 CharS[MAXN];9 intF[MAXN][MAXN];Ten intCOST[MAXN]; One A intMain () - { -scanf"%d%d",&n,&m); the GetChar (); -scanf"%s", s); -Memset (F,0,sizeof(f)); - for(intI=0; i<n;i++) + { - GetChar (); + CharTEMPC; A intb; atscanf"%c%d%d",&tempc,&a,&b); -cost[tempc-'a']=min (A, b); - } - - for(intI=1; i<m;i++) - for(intj=i-1; j>=0; j--) in { -F[j][i]=min (f[j+1][i]+cost[s[j]-'a'], f[j][i-1]+cost[s[i]-'a']); to if(S[j]==s[i]) f[j][i]=min (f[j][i],f[j+1][i-1]); + } -cout<<f[0][m-1]<<Endl; the return 0; *}
"Dynamic planning" poj3280-cheapest palindrome