Cheapest Palindrome Topic Links:
http://poj.org/problem?id=3280
Test instructions
Given a string of only lowercase letters, you can add or delete some letters (both add and delete cost and vary) to change the string to the minimum cost of a palindrome string.
Exercises
Set DP[I][J] is the minimum cost of changing the interval [i,j] into a palindrome, then two cases
① obvious, when S[i]==s[j], dp[i][j]=dp[i+1][j-1]
② when S[i]!=s[j], Dp[i][j] has four possible values, interval [i,j-1] Delete s[i], add s[j-1 on the right of S[i], interval [i+1,j] Delete s[j], add s[i+1 on the left side of S[j], then dp[i][j]= Min (Dp[i][j-1]+del[i],dp[i][j-1]+add[i],dp[i+1][j]+del[j],dp[i+1][j]+add[j]);
Code
#include<stdio.h>#include<string.h>Constint N=2001;int DP[N][n],cost1[26],cost2[26];Char s[N],a[2];IntMmin(int x,int y){return x<y? x: Y;}voidSolve(){int nMBC;Memset(DP,0,sizeof(DP));While(~scanf("%d%d", &n, &m)){scanf('%s ', S+1);For(int I=1; I<=n; ++i){scanf("%s%d%d"A, &b, &c); Cost1[A[0]-A]=b; cost2[A[0]-A]=c;}For(int Len=1; Len<m; ++len){For(int I=1; I+len<=m; ++i){Int J=i+len;If(s[I]==s[j]) DP[I][j]=dp[I+1][j-1];Else{b=dp[I+1][j]+Mmin(Cost1[s[I]-A],cost2[s[I]-A]); c=dp[I][j-1]+Mmin(Cost1[s[j]-A],cost2[s[j]-A]);DP[I][j]=mmin (B,c}}} Printf ( "%d\n ",dp[1][m}}int main< Span class= "Sh_symbol" > () {solve (); Span class= "Sh_keyword" >return 0;}
POJ 3280:cheapest palindrome interval dp good problem