Topic description
Given a string, it can be modified, deleted, increased operation, the corresponding operation has a corresponding cost, you need to use the minimum cost to turn the string into a palindrome
Topic procedure
First of all, floyed the minimum cost of various operations to find out, and then is similar to the editor distance DP, the problem of the pit for a long time ... The middle result will explode int, I set the inf=0x3f3f3f3f, the middle result has inf+inf+inf. The first DP array is of type int ... Here's a couple of things to find out ... Xi ' An live game also encountered this problem ... And then it was a waste of almost 100 minutes ... It's not enough time to do the problem ...
Code:
1 #defineMAXN 302 intCHANGECOST[MAXN][MAXN], ADDCOST[MAXN], ERASECOST[MAXN];3LL dp[ -][ -];4 classPalindromizationDiv15 {6 Public:7 intGetminimumcost (stringWord, vector <string>operations)8 {9memset (Changecost,0x3f,sizeof(Changecost));Tenmemset (Addcost,0x3f,sizeof(Addcost)); Onememset (Erasecost,0x3f,sizeof(Erasecost)); A for(inti =0; I < operations.size (); i++) - { - StringStream SS (Operations[i]); the strings; - CharA, B; - intnum; -SS >>s; + if(s = ="Add") - { +SS >> a >>num; AAddcost[a-'a'] =num; at } - if(s = ="Erase") - { -SS >> a >>num; -Erasecost[a-'a'] =num; - } in if(s = =" Change") - { toSS >> a >> b >>num; +Changecost[a-'a'][b-'a'] =num; - } the } * for(inti =0; I < -; i++) Changecost[i][i] =0; $ for(intK =0; K < -; k++)Panax Notoginseng for(inti =0; I < -; i++) - for(intj =0; J < -; J + +) the { + if(i = = J | | j = = k | | i = = k)Continue; AChangecost[i][j] = min (Changecost[i][j], changecost[i][k] +changecost[k][j]); the } + - for(inti =0; I < -; i++) $ for(intj =0; J < -; J + +) $ { -Addcost[i] = min (Addcost[i], addcost[j] +changecost[j][i]); -Erasecost[i] = min (Erasecost[i], changecost[i][j] +erasecost[j]); the } - intn =word.size ();WuyiMemset (DP,0x3f,sizeof(DP)); the for(inti = n-1; I >=0; i--) - { WuDp[i][i] = dp[i][i-1] =0; - for(intj = i +1; J < N; J + +) About { $ intA = Word[i]-'a', B = word[j]-'a'; -Dp[i][j] = min (Dp[i][j], dp[i +1][J] +Erasecost[a]); -Dp[i][j] = min (dp[i][j], dp[i][j-1] +erasecost[b]); - for(intK =0; K < -; k++) A { +Dp[i][j] = min (Dp[i][j], dp[i +1][J] + addcost[k] +changecost[a][k]); theDp[i][j] = min (dp[i][j], dp[i][j-1] + Addcost[k] +changecost[b][k]); -Dp[i][j] = min (Dp[i][j], dp[i +1][j-1] + Changecost[a][k] +changecost[b][k]); $ } the } the } theLL ret = dp[0][n-1]; the returnRET >= INF? -1: (int) ret; - } in};View Code
SRM 509 DIV1 500pt (DP)