Poj3280--cheapest palindrome (interval dp)

Source: Internet
Author: User

Cheapest Palindrome
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6186 Accepted: 3014

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 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

Hint

If We insert an "a" in the end to get "ABCBA", the cost would is 1000. If we delete the "a" on the beginning-get "BCB", the cost would is 1100. If we insert "BCB" at the begining of the string, the cost would are + + + + =, which is the minimum.

Given a string, each character is added or deleted with a cost, asking to change the string to the minimum cost of a palindrome string.

Dp[i][j] Represents the string from I to J of this substring to the minimum cost of palindrome string, by dp[i][j], add or remove a character on both sides can be obtained,

Dp[i-1][j],dp[i][j+1], if str[i-1]==str[j+1] then get it, dp[i-1][j+1]

The result of the final dp[0][m-1]

#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define INF 0x3f3f3f3f# Define LL __int64struct node{int k1, K2;} P[30];char str[2100];    LL dp[2100][2100]; int main () {int n, m, X, K1, K2, I, J, l;    Char ch;    scanf ("%d%d", &n, &m);    scanf ("%s", str);        for (i = 0; i < n; i++) {GetChar ();        scanf ("%c%d%d", &ch, &k1, &AMP;K2);        x = ch-' a '; P[x].k1 = K1;    P[x].k2 = K2;    } memset (Dp,inf,sizeof (DP));        for (i = 0; i < m; i++) {dp[i][i] = 0;    if (I+1 < m && str[i] = = str[i+1]) dp[i][i+1] = 0;            } for (l = 0, L < m; l++) {for (i = 0; I <= m-l; i++) {j = i+l-1;            if (i > 0) dp[i-1][j] = min (dp[i-1][j],min (p[str[i-1]-' A '].k1,p[str[i-1]-' a '].k2) +dp[i][j]); if (J < m) dp[i][j+1] = min (dp[i][j+1],min (p[str[j+1]-' a '].k1,p[str[j+1]-' a '].k2) +dp[i][j]); if (i > 0 && J < m && str[i-1] = = str[j+1]) dp[i-1][j+1] = min (dp[i-1][j+1],dp[i][j        ] ) ;        }}/*for (i = 0; i < m; i++) {for (j = 0; J < m; j + +) printf ("%i64d", Dp[i][j]);    printf ("\ n");    }*/printf ("%i64d\n", dp[0][m-1]); return 0;}

Poj3280--cheapest palindrome (interval 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.