Topic Link: [POJ 3280]cheapest PALINDROME[DP]
The analysis:
There is a string of length m, all lowercase letters, given the cost of deletion and insertion of each letter in the string, ask: to constitute a palindrome string, at least how many costs.
Ideas for solving problems:
Set Dp[i][j] for the string s[i ~ J] to become a palindrome string the minimum cost. So there's the transfer equation:
DP[I][J] = dp[i + 1][j-1] (s[i] = = S[j]);
Otherwise dp[i][j] Add or remove s[i by Dp[i + 1][j], add or remove dp[i][j-1 from s[j]. The former new characters for S[i], the latter new characters for S[j], and S[i + 1][j] and s[i][j-1] have been palindrome, so the transfer.
Personal experience:
The state has, but I have been entangled in the details of the internal, such as it deleted, it added, then the subsequent state of deletion add is not to consider the previous state. In fact, not to consider so much, because the transfer of the second has been all the added delete considered.
The specific code is as follows:
#include <cstdio>
#include <iostream>
#define LL long
using namespace std;
const int MAXN = 2e3 + +;
ll DP[MAXN][MAXN];
Char SH[MAXN];
int S[MAXN];
ll ad[30], del[30];
int main ()
{
int n, m; scanf ("%d%d%s", &n, &m, SH);
for (int i = 0; i < m ++i) s[i] = sh[i]-' a ';
Char c[2];
int a, B;
for (int i = 0; i < n; ++i) {
scanf ("%s%d%d", C, &a, &b);
Ad[c[0]-' a '] = A;
Del[c[0]-' a '] = b;
}
for (int i = m-1 i >= 0;-i) {for
(int j = i + 1; j < m; ++j) {
if (s[i] = = S[j]) dp[i][j] = dp[i + 1 ][J-1];
else {
Dp[i][j] = min (dp[i + 1][j] + min (ad[s[i], del[s[i]),
dp[i][j-1] + min (ad[s[j], del[s[j]));
}
}
printf ("%lld\n", Dp[0][m-1]);
return 0;
}