As long as you find that adding a character and deleting one character is equivalent, it's a pretty naked interval dp.
Because adding a character x at the current position is equivalent to deleting the character X in his symmetrical position, so long as the deletion is considered, the deletion fee is the addition and deletion of the min
Set F[I][J] for the price from I to J, the length from small to large enumeration update on the line F[i][j]=min (F[i][j-1]+cost[s[j]],f[i+1][j]+cost[s[i]]), if S[I]==S[J] can and f[i+1][j-1 ] Take a min
cpp #include<iostream> #include<cstdio> using namespace std; const int N=2005; int m,n,a[30],f[N][N],x,y; char s[N],ch[10]; int main() { scanf("%d%d",&m,&n); scanf("%s",s+1); for(int i=1;i<=m;i++) { scanf("%s%d%d",ch,&x,&y); a[ch[0]-‘a‘]=min(x,y); } for(int i=1;i<=n;i++) s[i]-=‘a‘,f[i][i]=0,f[i+1][i]=0; for(int l=1;l<=n;l++) for(int i=1;i+l<=n;i++) { int j=i+l; f[i][j]=min(f[i][j-1]+a[s[j]],f[i+1][j]+a[s[i]]); if(s[i]==s[j]) f[i][j]=min(f[i][j],f[i+1][j-1]); } printf("%d\n",f[1][n]); return 0; } /* 3 4 abcb a 1000 1100 b 350 700 c 200 800 */
Bzoj 1710: [Usaco2007 open]cheappal cheap palindrome "Interval DP"