"bzoj1710" [Usaco2007 open]cheappal cheap palindrome

Source: Internet
Author: User

"bzoj1710" [Usaco2007 open]cheappal cheap palindrome description

To keep track of all the cows, farmer John installed an automatic system on the farm. He gave every cow an electronic grade. When cows walk through this system, the names of the cows will be automatically read into. The electronic name of each cow is a string of a length of M (1 <= m <= 2,000) of different letters consisting of N (1 <= N <= 26). Soon, the naughty cows found the system's loophole: they could walk backwards through the barcode reader. A name of "ABCBA" does not cause any problems, but a cow named "ABCB" will turn into two cows ("ABCB" and "BCBA"). The farmer John wanted to change the name of the cow so that the bull's name was read as well as backwards. For example, "ABCB" can be added by "a" at the tail. Other methods Package Add "BCB" to the head, get "BCBABCB" or remove "a" and get "BCB". John can add or remove letters anywhere. Because the name is electronic, there is a fee for adding and deleting letters. There is a fee for adding and deleting each letter (0 <= fee <= 10,000). For the cost of adding or removing letters with the name of a cow, find the minimum fee to modify the name. An empty string is also a valid name.

Input

* First line: Two numbers separated by a space, N and M.

* Second line: M self-symbol, the initial name of the cow.

* 3rd ... N+2 Line: Each line contains one letter and two integers, each of which is the cost of adding and removing the letter.

Output

An integer that changes the minimum cost of an existing name.

Sample Input3 4
Abcb
A 1000 1100
B 350 700
C 200 800
Input explanation:
The name is "ABCB" and the operating costs are as follows:
Add Delete
A 1000 1100
B 350 700
C 200 800
Sample Output900
Output Explanation:
The cost of adding "a" at the tail to "ABCBA" is 1000. Delete "A" on the head, the cost of getting "BCB" is 1100. Add "BCB" to the head to get the minimum cost, 350+200+350=900.
Exercises

Adding a number can be considered as deleting a number, then the problem is converted to a deleted palindrome string

N^2DP can

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <cmath>6 #defineF (i,j,n) for (int i=j;i<=n;i++)7 #defineD (i,j,n) for (int i=j;i>=n;i--)8 #definell Long Long9 #defineMAXN 2005Ten using namespacestd;  One CharS[maxn],ch;  A intM,n,x,y;  - intf[maxn][maxn],w[ -];  -InlineintRead () the {   -     intx=0, f=1;CharCh=GetChar ();  -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  -      while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();}  +     returnx*F;  - }   + intMain () A {   atM=read (); n=read ();  -scanf"%s", s+1);  -F (I,1, M) -     {   -scanf"%c", &ch); while(ch<'a'|| Ch>'Z') scanf ("%c",&ch);  -X=read (); y=read ();  inw[ch-'a']=min (x, y);  -     } toD (i,n-1,1) F (j,i+1, N) +     {   -F[i][j]=min (f[i+1][j]+w[s[i]-'a'],f[i][j-1]+w[s[j]-'a']);  the         if(S[i]==s[j]) f[i][j]=min (f[i][j],f[i+1][j-1]);  *     }   $printf"%d\n", f[1][n]); Panax Notoginseng}

"bzoj1710" [Usaco2007 open]cheappal cheap palindrome

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.