1/* 2 meaning: it takes a certain amount of money to add or delete a string of characters (all in lowercase letters. 3. What is the minimum cost of converting a string into a return string? 4 5 train of thought: if a character string can be added with a character X to form a return string, then deleting this character x 6 from this string can also form a return string! 7. Therefore, we only need to delete the records and add the minimum cost of this character X! -> The minimum cost of converting to the number of characters added to form a text return string! 8 9 STR [I]! = STR [k] 10 DP [I] [J] = min (DP [I] [J-1] + cost [STR [k]-'a'], DP [I + 1] [J-1] + cost [STR [I]-'a']); 11 12 STR [I] = STR [k] 13 DP [I] [J] = DP [I + 1] [J-2]; 14 15 */16 # include <iostream> 17 # include <cstring> 18 # include <cstdio> 19 # include <algorithm> 20 # define n 2005 21 using namespace STD; 22 23 int DP [N] [N]; 24 25 int cost [30]; 26 27 char STR [N]; 28 29 int main () {30 int M, N; 31 While (scanf ("% d", & M, & N )! = EOF) {32 scanf ("% s", STR + 1); 33 memset (cost, 0, sizeof (cost); 34 while (M --) {35 char ch; 36 int A, B; 37 getchar (); 38 scanf ("% C % d", & Ch, & A, & B ); 39 cost [CH-'a'] = min (a, B); 40} 41 for (INT I = 1; I <= N; ++ I) 42 DP [I] [1] = 0; 43 for (Int J = 2; j <= N; ++ J) 44 for (INT I = 1; I + J-1 <= N; ++ I) {45 int K = I + J-1; 46 If (STR [I]! = STR [k]) 47 DP [I] [J] = min (DP [I] [J-1] + cost [STR [k]-'a'], DP [I + 1] [J-1] + cost [STR [I]-'a']); 48 else DP [I] [J] = DP [I + 1] [J-2]; 49} 50 51 printf ("% d \ n ", DP [1] [N]); 52} 53 return 0; 54}
Nyoj 1023 or retrieval (DP, which forms a retrieval string at least cost)