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 Input 3 4
ABCB
b
input explanation:
add delete
a 1100
c
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. interval DP of the template problem. considering the fact that deleting a character is equivalent to adding one of these characters in a symmetric position, simply take the cost of the deletion and increment to take the min and only consider deleting the line. F[i][j] Indicates the minimum cost of i-j this interval into palindrome, because only the deletion is considered, so:f[i][j]=min (f[i+1][j]+cost[ch[i]-' A ' +1],f[i][j-1]+cost[ch[j]-' a ' +1]). in particular, when ch[i]==ch[j], can be transferred directly from f[i+1][j-1], so add a sentence:f[i][j]=min (F[i][j],f[i+1][j-1]). also note that the memory search will not appear re (possibly L>r, should return 0 at this time). Code:
#include<cstdio>#include<cstring>#include<algorithm>Const intn=2e3+Ten, inf=0x3f3f3f3f;usingstd::min;intN,m,f[n][n];Charch[n],c[3];intco[ -];intDfsintLintR) { if(L==R)return 0; if(f[l][r]!=-1)returnF[L][R];//This sentence must be addedf[l][r]=inf; F[L][R]=min (DFS (l +1, R) +co[ch[l]-'a'+1],dfs (l,r-1) +co[ch[r]-'a'+1]); if(Ch[l]==ch[r]) f[l][r]=min (F[l][r],dfs (l +1, R-1)); returnf[l][r];}intMain () {scanf ("%d%d",&n,&m); scanf ("%s", ch+1); for(intI=1, a,b;i<=n;i++) {scanf ("%s%d%d", c+1,&a,&b); co[c[1]-'a'+1]=min (A, b); } for(intI=1; i<=m;i++) for(intj=i;j<=m;j++) f[i][j]=-1; //can not directly memset, cause with the memory of the searchprintf"%d", DFS (1, M)); return 0;}bzoj1710
"bzoj1710/usaco2007 Open" Cheappal cheap palindrome--Interval DP