POJ 3280 Cheapest Palindrome(DP)
被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费。我觉得挺难的理解的。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 int dp[2015][2015]; 6 char s1[2011]; 7 int hash[201]; 8 int main() 9 { 10 int n,m,i,j,a,b; 11 char ch[3]; 12 while(scanf("%d%d",&n,&m)!=EOF) 13 { 14 scanf("%s",s1); 15 memset(dp,0,sizeof(dp)); 16 for(i = 1;i <= n;i ++) 17 { 18 scanf("%s%d%d",ch,&a,&b); 19 hash[ch[0]-'a'] = min(a,b); 20 } 21 for(i = m;i >= 1;i --) 22 { 23 for(j = i;j <= m;j ++) 24 { 25 if(s1[i-1] == s1[j-1]) 26 { 27 dp[i][j] = dp[i+1][j-1]; 28 } 29 else 30 { 31 dp[i][j] = min(dp[i+1][j]+hash[s1[i-1]-'a'],dp[i][j-1]+hash[s1[j-1]-'a']); 32 } 33 } 34 } 35 printf("%d\n",dp[1][m]); 36 } 37 return 0; 38 } 39 /* 40 2 5 41 bbaab 42 a 1 2 43 b 10 10 44 */