POJ 3280 Cheapest palindrome Dynamic programming method

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 lengthm (1≤ m ≤2,000) characters drawn from An alphabet of n (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) 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 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.

Source

Usaco Open Gold

A look at this problem is always felt to be a string processing problems, in fact, it is necessary to model dynamic programming method.

Dynamic programming method of modeling all feel is the most difficult to close, of course, the simplest is to refer to others, their own modeling is really difficult.

The modeling of the subject is the use of a two-dimensional array palin[i][j], which represents J characters, that is, if the starting point of the string is labeled I, then I to i+j-1 the minimum modified value of the character.

You can also use recursive thinking to move from this string to a smaller string.

The final optimizer uses a scrolling array to change the two-dimensional array Villivi.

The following procedure makes detailed comments:

#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],id array represents the minimum modified value computed for the current D-segment,! The ID array represents the computed value of the d-1 segment, so if the array of ID dimensions is not evaluated, the minimum modified value for the d-2 character segment is calculated as int cost[alp_num];//only need to calculate the smallest modified value can be 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));//initial value is zero, There is only one character when it is definitely not necessary to modify the palindrome, the modified value is zero bool id = 0;for (int d = 2; d <= M; d++) {id =!id;for (int i = 0, j = d-1; J < M; i++, J + +) {//only require uniform recording method, I record the current change of D character//two characters equal, then the modified value if (cow[i] = cow[j]) directly equal to d-2 character if [] = Palin[id][i]; else//two characters unequal compare the processing of these two characters which is cheap {//palin[!id][i+1] represents the processing value of i+1 to i+1+d-1 characters//So does not contain the I-character int c1 = palin[!id][i+1] + cost[cow[i] -' A '];//palin[!id][i] represents the processing value of I to i+d-1 characters//therefore does not contain the first j character int c2 = Palin[!id][i] + cost[cow[j]-' a '];//palin[id][i] for I to i+ The processing value of the D-character palin[id][i] = min (c1, c2);}} return palin[id][0];} int main () {int N, M, A, d;while (scanf ("%d%d", &n, &m)! = EOF) {GetChar (); get (cow);//gets handles the \ n character after it (cost, cost+alp_num, 0); for (int i = 0; i < N; i++) {Char ch = getchar (); scanf ( "%d%d", &a, &d);//insertion or deletion of the processing character cost cost[ch-' a '] = min (A, d);//Only need to record the minimum cost of processing characters can be getchar ();//dispose of the following \ n characters}printf (" %d\n ", Getmincost (M));} return 0;}



POJ 3280 Cheapest palindrome Dynamic programming method

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.