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.
Exercises
For a palindrome string, add a character, and delete the original character is equivalent, so the price can take min.
Then the interval DP, ' O (n^2) '.
Code:
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>//by Zrt//problem:using namespaceStd;typedefLong LongLL;Const intInf0x3f3f3f3f);Const DoubleEPS (1e-9);intn,m,cost[ -];Chars[2100];intf[2100][2100];intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin); Freopen ("OUT.txt","W", stdout); #endifscanf ("%d%d%s", &n,&m,s+1); for(intI=1, x,y;i<=n;i++){ Chartmp[5]; scanf ("%s%d%d",tmp,&x,&y); cost[tmp[0]-'a']=min (x, y); } memset (F,0x3f,sizeoff); for(intI=1; i<=m;i++) s[i]-='a', f[i][i]=0, f[i+1][i]=0; for(intL=1; l<=m;l++){ for(intI=1; i<=m;i++){ intj=i+l; if(j>m) Break; F[I][J]=min (f[i][j-1]+cost[s[j]],f[i+1][j]+Cost[s[i]]); if(S[i]==s[j]) f[i][j]=min (f[i][j],f[i+1][j-1]); }} printf ("%d\n", f[1][m]); return 0;}
Bzoj 1710: [Usaco2007 open]cheappal cheap palindrome