Text return string of small MB

Source: Internet
Author: User
Description

Small M prefers to return to the text string, which is a string that reads the same from left to right and from right to left.

Now the small x finds the small M and gives it a string S, hoping that the small M will convert it into a return string.

Small m can perform the following three types of operations:

  1. Add add a character anywhere in the string
  2. Erase deletes a character
  3. Change one letter to another to change a character to another

However, each operation can only be valid for specified characters. For example, small m can only allow erase 'A', add 'B', change 'C' to 'D', and no other operations can be used. There is no free lunch in the world, and every small M operation has a certain cost.

Input Format

The first line is a string that represents the string to be modified by the small M ( Length ≤ 50 )

The number N in the second row indicates the operations that small m can use ( N ≤ 50 )

The next n rows have one operation per row, which can be used in the following three forms:

"Add C x": the cost of adding a character C to a string is X.

"Erase C x": indicates that it takes X to delete a character C from the string.

"Change C1 C2 X": the cost of converting a character C1 to C2 is X.

Note: "Change C1 C2 X" cannot change C2 to C1

Yes X ≤ 100000, C1 <> C2 ,
And any two rows have different operations.

Output Format

A row with one number indicates that the minimum cost of converting the initial string to the input string is M. If not, output-1

Sample Input
caaaaaab6change b a 100000change c a 100000change c d 50000change b e 50000erase d 50000erase e 49999
Sample output
199999

Problem: several groups of data cannot be used. I don't know why ···. Paste the code first and then think about it.

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; char s [100]; int ch [40] [40], er [40], add [40]; int DP [100] [100]; const int INF = 999999999; void Init () {for (INT I = 0; I <26; I ++) {er [I] = add [I] = inf; For (Int J = 0; j <26; j ++) ch [I] [J] = (I = J? 0: INF) ;}} void Floyd () {int I, j, k; For (k = 0; k <26; k ++) for (I = 0; I <26; I ++) for (j = 0; j <26; j ++) CH [I] [J] = min (CH [I] [J], ch [I] [k] + CH [k] [J]); for (I = 0; I <26; I ++) for (j = 0; j <26; j ++) if (CH [I] [J]! = Inf) {er [I] = min (ER [I], CH [I] [J] + er [J]); add [I] = min (add [I], CH [I] [J] + Add [J]) ;}} int operdp () {int I, J; int Len = strlen (s); memset (DP, 0, sizeof (DP); for (I = 0; I <Len; I ++) for (j = I + 1; j <Len; j ++) DP [I] [J] = inf; for (I = len-2; I> = 0; I --) {for (j = I + 1; j <Len; j ++) {If (s [I]! = S [J]) {If (ER [s [I]-'a']! = Inf) // Delete s [I] DP [I] [J] = min (DP [I + 1] [J] + er [s [I]-'A' on the left end' ], DP [I] [J]); If (add [s [I]-'a']! = Inf) // Add s [I] DP [I] [J] = min on the right end (DP [I + 1] [J] + Add [s [I]-'a'], DP [I] [J]); If (ER [s [J]-'a']! = Inf) // Delete s [J] DP [I] [J] = min (DP [I] [J-1] + er [s [J]-'a'] on the right end, DP [I] [J]); If (add [s [J]-'a']! = Inf) // Add s [J] DP [I] [J] = min (DP [I] [J-1] + Add [s [J]-'a'] on the left end, DP [I] [J]); If (CH [s [I]-'a'] [s [J]-'a']! = Inf) DP [I] [J] = min (DP [I + 1] [J-1] + CH [s [I]-'a'] [s [J]-'A' ], DP [I] [J]); // change the left end to the same as the right end if (CH [s [J]-'a'] [s [I]-'a']! = Inf) DP [I] [J] = min (DP [I + 1] [J-1] + CH [s [J]-'a'] [s [I]-'A' ], DP [I] [J]); // change the right end to the same as the left end} else {If (ER [s [I]-'a']! = Inf) // Delete s [I] DP [I] [J] = min (DP [I + 1] [J] + er [s [I]-'A' on the left end' ], DP [I] [J]); If (ER [s [I]-'a']! = Inf) // Add s [I] DP [I] [J] = min on the right end (DP [I + 1] [J] + Add [s [I]-'a'], DP [I] [J]); If (ER [s [J]-'a']! = Inf) // Delete s [J] DP [I] [J] = min (DP [I] [J-1] + er [s [J]-'a'] on the right end, DP [I] [J]); If (ER [s [J]-'a']! = Inf) // Add s [J] DP [I] [J] = min (DP [I] [J-1] + Add [s [J]-'a'] on the left end, DP [I] [J]); DP [I] [J] = min (DP [I + 1] [J-1], DP [I] [J]) ;}}if (DP [0] [len-1] = inf) Return-1; return DP [0] [len-1];} int main () {int N, V; char c1 [3], C2 [3], clerk [100]; while (scanf ("% s", S )! = EOF) {Init (); scanf ("% d", & N); While (n --) {scanf ("% s", seconds ); if (strcmp (separator, "change") = 0) {scanf ("% S % d", C1, C2, & V ); ch [c1 [0]-'a'] [c2 [0]-'a'] = V;} else if (strcmp (separator, "erase") = 0) {scanf ("% S % d", C1, & V); er [c1 [0]-'a'] = V ;} else {scanf ("% S % d", C1, & V); add [c1 [0]-'a'] = V ;}} Floyd (); printf ("% d \ n", operdp ();} return 0 ;}

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.