[POJ 3280] Cheapest PALINDROME[DP]__DP

Source: Internet
Author: User
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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.