Cheapest Palindrome
Time limit:2000ms Memory limit:65536kb 64bit IO Format:%lld &%llu
Submit
Status
Description
Keeping track of the cows can be a tricky tasks so farmer John has installed a system to automate it. He has installed in each cow a electronic ID tagged that the system would read as the cows pass by a scanner. Each ID tag "s contents are currently a single string with length M (1≤m≤2,000) characters drawn from a alphabet of N (1≤n≤26) different symbols (namely, lower-case Roman).
Cows, being the mischievous creatures they are, sometimes try to spoof the system by walking. 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 two different IDs ("ABCB" and "BCBA").
FJ would like-change the cows's ID tags so they read the same no matter which direction that cow by. For example, ' ABCB ' can be changed by adding ' a ' at the ' End of ' to ' form ' ABCBA ' so ' the ID is palindromic (reads the same Forwards and backwards). Some the "Ways" to "the" is palindromic are include adding the three letters "BCB" to the begining to yield the ID "BCBABCB" or removing the letter "a" to yield the id "BCB". One can add or remove characters at no location in the string yielding a string longer or shorter than the original Strin G.
Unfortunately as the ID tags are electronic, each character insertion or deletion has-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, find the Mini Mum cost to change the ID of tag so it satisfies FJ ' s requirements. An empty ID, tag is 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:n and M
line 2:this line contains exactly M characters which con Stitute the initial ID string
Lines 3. N+2:each line contains three space-separated entities:a character of the input alphabet and two integers which are Respe Ctively the cost of adding and deleting that character.
Output
Line 1: "A single" with "a single" is the minimum of the given name tag.
Sample Input
3 4
ABCB
A 1000 1100
B-M
C-
Sample Output
900
Hint
If We insert an ' a ' on the ' end of ' to get ' ABCBA ', the cost would is 1000. If we delete the "a" on the "BCB" of "beginning", the cost would is 1100. If we insert "BCB" at the begining of the string and the cost would to be--+ + = 900, which is the minimum.
Source
Usaco 2007 Open Gold
The main effect of the topic:
A string that inserts or deletes a character that needs a certain amount of money, asking the minimum price of the string to become a palindrome string.
DP[I][J], representing the minimum cost of a string i,j into a palindrome (a sudden sense of the meaning of a DP equation there is a math teacher who asks what to set what Qaq)
Min (DP[I+1][J]+TMP1,DP[I][J-1]+TMP2)
TMP1 add I to the right of the I character, or decrease the minimum value of I on the left
TMP2 Empathy
If S[i]==s[j],,min (Dp[i+1][j-1],...)
Note: You can look at the initial value of dp[][], the initial value of the DP equation is very beautiful ... Beautiful...
Code:
#include <iostream> #include <cstdio> #include <ctime> #include <cstdlib> #include <cmath > #include <cstring> #include <string> #include <set> #include <map> #include <vector> include<queue> #include <algorithm> #ifdef WIN32 #define AUTO "%i64d" #else #define AUTO "%LLD" #endif #define INF 0x3f3f3f3f #define CLOCK clocks_per_sec #define CLE (x) memset (x,0,sizeof (x)) #define MAXCLE (x) memset (x,127,sizeof (x ) #define MINCLE (x) memset (x,-1,sizeof (x)) #define MINN (x1,x2,x3) min (x1,min (X2,X3)) #define COP (A,X) memcpy (X,a, sizeof (a)) #define FROP "POJ" #define C (a,b) next_permutation (a,b) #define LL long #define SMIN (x,tmp) x=min (x,tmp) u
Sing namespace std;
const int n=2005,m=28;
Char S[n];
int Add[m],del[m],n,m,len;
void Init () {scanf ("%d%d", &n,&m);
scanf ("%s", s+1);
Len=strlen (s+1);
GetChar ();
for (int i = 1; i<= n; i++) {char ch;
scanf ("%c", &ch);
int x,y; scanf ("%d%d", &x,&y);
add[ch-' a ' +1]=x;
del[ch-' a ' +1]=y;
GetChar ();
} int dp[n][n];
int main () {Freopen (frop ". In", "R", stdin);
Freopen (Frop ". Out", "w", stdout);
Init ();
for (int k = 1; k<=len;k++)//We can know that the equation is from i+1,j or i,j-1, or i+1,j-1, in short the digits are smaller.
for (int i =1,j=k;i<=len&&j<=len;i++,j++) {dp[i][j]=inf;
int Tmp1=min (add[s[i]-' A ' +1],del[s[i]-' a ' +1]);
int Tmp2=min (add[s[j]-' A ' +1],del[s[j]-' a ' +1]);
Dp[i][j]=min (DP[I+1][J]+TMP1,DP[I][J-1]+TMP2);
if (S[i]==s[j]) dp[i][j]=min (dp[i][j],dp[i+1][j-1]);//a character, or two characters, i+1,j-1, and will not =inf, dislocation is 0, so the initial value is not set, when there are 3, 1 of the already 0.
printf ("%d", Dp[1][len]);
return 0;
}