POJ3280 cheapest palindrome "DP"

Source: Internet
Author: User

Cheapest Palindrome
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6013 Accepted: 2933

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 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 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) 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 GoldDp[i][j] Represents the minimum cost required to convert from the position of the string I to position J to palindrome, when Str[i]==str[j], dp[i][j]=dp[i+1][j-1]; otherwise dp[i][j]=min (dp[i+1][j]+cost [i], dp[i][j-1]+cost[j]);

#include <stdio.h> #include <string.h> #define MAXN 2010char str[maxn];int dp[maxn][maxn];struct Node {int a DD, Del;} Cost[26];int min (int a, int b) {return a < b a:b;} int Getdele (char ch) {return cost[ch-' a '].del;} int Getadd (char ch) {return cost[ch-' a '].add;}    int main () {//Freopen ("Stdin.txt", "R", stdin);    int N, M, I, J, id, step, len;    Char buf[2];    scanf ("%d%d", &n, &m);    scanf ("%s", str);    memset (Cost,-1, sizeof);        while (m--) {scanf ("%s", buf);        id = buf[0]-' a ';    scanf ("%d%d", &cost[id].add, &cost[id].del);    } len = strlen (str);                for (step = 1, step < Len; ++step) for (i = 0; i + step < len; ++i) {if (str[i] = = Str[i+step])            Dp[i][i+step] = dp[i+1][i+step-1]; else {Dp[i][i+step] = min (min (Dp[i+1][i+step] + getdele (Str[i]), dp[i][i+step-1] + getdele (str[i+step)), Min (Dp[i+1][i+step] + getadd (Str[i]), dp[i][i+step-1] + getadd (Str[i+step]));    }} printf ("%d\n", dp[0][len-1]); return 0;}


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

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