POJ 3280 Cheapest Palindrome dynamic programming method, pojpalindrome

Source: Internet
Author: User

POJ 3280 Cheapest Palindrome dynamic programming method, pojpalindrome

Description

Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automatic it. he has installed on each cow an electronic ID tag that the system will read as the cows pass by a packet. each ID tag's contents are currently a single string with lengthM(1 ≤M≤ 2,000) characters drawn from an alphabetN(1 ≤N≤26) different symbols (namely, the lower-case roman alphabet ).

Cows, being the mischievous creatures they are, sometimes try to spoof the system by walking backwards. while a cow whose ID is "abcba" wocould read the same no matter which direction the she walks, a cow with the ID "abcb" can potentially register as two different IDs ("abcb" and "bcba ").

FJ wowould like to change the cows's ID tags so they read the same no matter which direction the cow walks. for example, "abcb" can be changed by adding "a" at the end to form "abcba" so that the ID is palindromic (reads the same forwards and backwards ). some other ways to change the ID to be palindromic are include adding the three letters "bcb" to the ining to yield the ID "bcbabcb" or removing the letter "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 string.

Unfortunately as the ID tags are electronic, each character insertion or deletion has a cost (0 ≤Cost≤ 10,000) which 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 minimum cost to change the ID tag so it satisfies FJ's requirements. an empty ID tag is considered to satisfy the requirements of reading the same forward and backward. only letters with associated costs can be 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 two integers which are respectively the cost of adding and deleting that character.

Output

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

Sample Input

3 4abcba 1000 1100b 350 700c 200 800

Sample Output

900

Hint

If we insert an "a" on the end to get "abcba", the cost wocould be 1000. if we delete the "a" on the beginning to get "bcb", the cost wocould be 1100. if we insert "bcb" at the begining of the string, the cost wocould be 350 + 200 + 350 = 900, which is the minimum.

Source

USACO 2007 Open Gold

At first glance, I think this question is a string processing problem. It is actually a question that requires modeling dynamic programming.

The modeling of dynamic programming is all the most difficult. Of course, the simplest is to refer to others. It is really difficult to build your own models.

The modeling of this question is to use a two-dimensional array palin [I] [j] To represent j characters. If the start subscript of a string is I, then what is the minimum modification value for I to I + J-1 characters.

You can also use recursive thinking to recursive this string step by step to a smaller string.

The final optimization program uses a rolling array to change the dimension of a two-dimensional array.

The following procedure provides detailed Annotations:

# Include <stdio. h> # include <string. h> # include <limits. h ># include <algorithm> using namespace std; const int MAX_M = 2001; const int ALP_NUM = 26; int palin [2] [MAX_M]; // palin [id] [I]. The id array represents the minimum modification value calculated for the current d character segments ,! The id array represents the calculated value of the D-1 character segment. If the array of the id dimension is not calculated, it represents the minimum modified value calculated by the D-2 character segment int cost [ALP_NUM]; // you only need to calculate the minimum modification value for the char cow [MAX_M]; // id string int getMinCost (int M) {memset (palin [0], 0, sizeof (int) * (M + 1); memset (palin [1], 0, sizeof (int) * (M + 1); // The initial value is zero, when there is only one character, it must be the Palindrome that does not need to be modified. The value is changed to 0 bool id = 0; for (int d = 2; d <= M; d ++) {id =! Id; for (int I = 0, j = D-1; j <M; I ++, j ++) {// you only need to unify the record method, I record the change value of the current d character. // if (cow [I] = cow [j]) palin [id] [I] = palin [id] [I + 1]; else // if the two characters are not equal, it is cheaper to compare and process the two characters {// palin [! Id] [I + 1] indicates the processing value from I + 1 to I + 1 + D-1. // Therefore, it does not contain the I-th character int c1 = palin [! Id] [I + 1] + cost [cow [I]-'a']; // palin [! Id] [I] indicates the processing value from I to I + D-1. // Therefore, it does not contain the j-character int c2 = palin [! Id] [I] + cost [cow [j]-'a']; // palin [id] [I] indicates the processing value from I to I + d. palin [id] [I] = min (c1, c2 );}}} return palin [id] [0];} int main () {int N, M, a, d; while (scanf ("% d", & N, & M )! = EOF) {getchar (); gets (cow); // fill (cost, cost + ALP_NUM, 0) is processed after gets ); for (int I = 0; I <N; I ++) {char ch = getchar (); scanf ("% d", & a, & d ); // The cost of the inserted or deleted processing characters. cost [ch-'a'] = min (a, d ); // getchar () can be used to record the minimum cost of processing characters; // process the \ n character} printf ("% d \ n ", getMinCost (M);} 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.