1710: [Usaco2007 open]cheappal cheap palindrome time limit: 5 Sec Memory Limit: MB
Submit: 466 Solved: 262
[Submit] [Status] [Discuss] Description
To keep track of all the cows, the farmer John put up a set of his own active system on the farm. He gave each cow an electronic grade. When cows walk through this system, the name of the cow will be read on its own initiative. The electronic name of each cow is a string of a length of M (1 <= m <= 2,000) consisting of a different letter of N (1 <= N <= 26). Very fast, naughty cows find a system flaw: they can walk backwards through the barcode reader. A name of "ABCBA" does not cause any problems, but the cow named "ABCB" becomes two cows ("ABCB" and "BCBA"). The farmer John wanted to change the name of the cow so that the bull's name was read and reversed. For example, "ABCB" can be added "a" by the tail. Other methods include adding "BCB" to the head, getting "BCBABCB" or removing "a" and getting "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 and all, find the minimum fee to change 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 deleting the letter.
Output
An integer that changes the minimum cost of an existing name.
Sample Input
3 4
Abcb
A 1000 1100
B 350 700
C 200 800
Input explanation:
The name is "ABCB", operating costs such as the following:
Add Delete
A 1000 1100
B 350 700
C 200 800
Sample Output
900
Output Explanation:
The cost of adding "a" to "ABCBA" at the end of the trailer is 1000. Delete "A" on the head, get "BCB" cost is 1100. Adding "BCB" to the head will get the minimum cost, 350+200+350=900.
HINT
Source
Gold
A simple DP question ... However, I still did not think of a way.
First we can find that the function of deleting and adding a letter is the same, so the weights of each letter only need to be assigned a small value of both.
Using f[i][j] to indicate the minimum cost to change from I to J to Palindrome, then the transfer equation is:
F[i][j]=min (F[i+1][j]+w[s[i]],f[i][j-1]+w[s[j]).
Suppose S[i]=s[j]. F[i][j]=min (F[i][j],f[i+1][j-1]).
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #define F (i,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) #define LL long Long#define MAXN 20 05using namespace Std;char s[maxn],ch;int m,n,x,y;int f[maxn][maxn],w[30];inline int read () {int X=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int main () {m=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 (); w[ch-' A ']=min (x, y);} D (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 ']), if (S[i]==s[j]) f[i][j]=min (f[i][j ],f[i+1][j-1]);} printf ("%d\n", F[1][n]);}
bzoj1710 "Usaco2007 Open" Cheappal cheap palindrome