POJ 3280 cheapest palindrome (DP)

Source: Internet
Author: User



Description

Keeping track of all the cows can is a tricky task so Farmer John have installed a system to automate it. He has installed on each cow an electronic ID tag that the system would read as the cows pass by a scanner. Each ID tag ' s contents is currently a single string with length m (1≤ m ≤2,000) characters drawn from An alphabet ofn (1≤ n ≤26) different symbols (namely, the lower-case Roman alphabet).

Cows, being the mischievous creatures they is, sometimes try to spoof the system by walking backwards. While a cow whose id was "ABCBA" would read the same no matter which direction the She walks, a cow with the ID "ABCB" can Potentially register as different IDs ("ABCB" and "BCBA").

FJ would like-to-change the cows's ID tags so they read the same no matter which direction the cow walks by. For example, "ABCB" can being changed by adding ' a ' at the end to form ' ABCBA ' so ' the ID is palindromic (reads the same Forwards and backwards). Some other ways to change the ID of be palindromic is include adding the three letters "BCB" to the begining to yield the ID "BCBABCB" or removing the "a" to yield the id "BCB". One can add or remove characters at any location in the string yielding a string longer or shorter than the original Strin G.

Unfortunately as the ID tags is electronic, each character insertion or deletion have a cost (0≤ cost ≤10,000) W Hich varies depending on exactly which character value to be added or deleted. Given the content of a cow ' s ID tag and the cost of inserting or deleting each of the alphabet ' s characters, find the Mini Mum cost to change the ID tag so it satisfies FJ ' s requirements. An empty ID tag was considered to satisfy the requirements of reading the same forward and backward. Only letters with associated costs can is added to a string.

Input

Line 1:two space-separated integers: Nand M
Line 2:this line contains exactly MCharacters which constitute the initial ID string
Lines 3.. N+2:each line contains three space-separated entities:a character of the input alphabet and both integers which are respec Tively the cost of adding and deleting that character.

Output

Line 1: A single line with a single integer this is the minimum cost-to-change the given name tag.

Sample Input

3 4ABCBA 1100b 700c 200 800

Sample Output

900
Test instructions: A series of alphabetic sequences. The addition or deletion of a character makes the sequence a palindrome, and it costs to add and subtract, asking for the least amount.

Set DP[I][J] for the cost from I to J.
Dp[i][j] = min (Dp[i+1][j]+cost[i], dp[i][j-1]+cost[j]);  (A[i]! = A[j])
DP[I][J] = Dp[i+1][j-1] (a[i] = a[j])
Cost[] is the small value of each character deletion or addition, because deleting a[i] and adding a a[i after J) effect is the same, only need to compare the cost of the two who is smaller
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h>using namespace Std;typedef Long Long ll;const int max=0x3f3f3f3f;int N,m,cost[30],dp[2007][2005];char s[2005],cc[3];int main () {    scanf ("%d%d%s", &n,&m,s);    for (int i=0;i<n;i++) {        int xx,yy;        scanf ("%s%d%d", cc,&xx,&yy);        cost[cc[0]-' a '] = min (xx,yy);    }    for (int j=1;j<m;j++) for        (int i=j-1;i>=0;i--)             if (s[i] = = S[j]) dp[i][j] = dp[i+1][j-1];             else Dp[i][j] = min (dp[i+1][j]+cost[s[i]-' a '), dp[i][j-1]+cost[s[j]-' a ']);    printf ("%d\n", Dp[0][m-1]);    return 0;}

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

POJ 3280 cheapest palindrome (DP)

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.