Test Instructions: An alphabetic sequence of length m, consisting of n letters, each with two costs: ① delete the letter; ② Add the letter. Ask the minimum cost of turning this sequence into a palindrome sequence. the f[I [j] represents the minimum cost of I-J this section into a palindrome string. when sequence s[i] = = s[J], no cost is required: f[I [j] = f[i + 1][j-1];otherwise:①-> plus or minus the I-letter, f[i + 1 [j] + w[s[I]];②-> plus or minus the J-letter, f[I [j-1] + w[s[j]];(because adding or deleting a letter is equivalent, the cost is minimized at the time of reading)CODE:
/*Author:JDDPROG:poj3280 cheapest palindoromedate:2015.10.9*/#include<cstdio>#defineREP (i, S, N) for (int i = s; i <= n; i + +)#defineRep_ (i, S, N) for (int i = n; i >= s; i-)#defineMax_n 2005using namespacestd;intN, M, F[max_n][max_n], w[ -];CharS[max_n];inlineintRead () {Charc =GetChar (); while(! (c >='0'&& C <='9')) C =GetChar (); intRET =0; while(c >='0'&& C <='9') ret = RET *Ten+ C-'0', C =GetChar (); returnret;}#defineMin (A, B) (a < b? a:b)inlinevoidinit () {n= Read (); m =read (); scanf ("%s", S +1); GetChar (); CharCintx, y; REP (i,1, N) {scanf ("%c%d%d", &c, &x, &y); GetChar (); W[c-'a'+1] =min (x, y); }}inlinevoiddoit () {rep_ (I,1, M-1) REP (j, i +1, M) { if(S[i] = = S[j]) F[I][J] = f[i +1][j-1]; ElseF[i][j] = min (f[i +1][J] + w[s[i]-'a'+1], F[i][j-1] + w[s[j]-'a'+1]); } printf ("%d\n"+ a[1][m]);}intMain () {init (); Doit (); return 0;}
POJ3280 Cheapest Palindrome