algorithm - Changing letters of a string to obtain maximum score -


You are given a string and can change the maximum number of characters in the string. You have also been given a list of substrings (two characters long), with a corresponding score. Each opportunity of the object within the string adds to your total score. What is the maximum possible receipt score?

string length & lt; = 150, Q & lt; = 100, the number of substrings & lt; = 700


Example:

string = BPDCG

Q = 2

Substring:

Bez - Score: 2

zd - Score: 5

DM - Score: 7

NG - Score: 10

This example In, you can get the maximum score by changing the "p" in the string, with a "z" and "c" with "n" on this, your new string is "bzdng", in which 2 + 5 + 10 = 17 Score is.

I know that a string has been given that the letters have already been changed, the score may be a dictionary matching algorithm such as eh-coarsic (or slightly wrong complexity, rabin Carp) was checked in linear time. However, it will take a lot of time to try every two letter replacement and then it will take a lot of time to investigate.

I thought one more possible way to work backwards, to create the ideal string from the given substrings and then check whether it is different from the original string mostly for two characters. However, I'm not sure how to do it, and even if this can be done, I think it will take a very long time.

What is the best way to go about this?

An effective way to resolve this is to use dynamic programming.

Any of the letters starts scoring-2 scoring, and a special letter "*" which stands for any other letter from them.

Let's score maximum (S, i, j, c) string using j substitution (up to index), where string is in character C (where l is in c) ends.

Repeat relationships are somewhat messy (or at least, I did not find one particularly beautiful formulation of them), but here are some code that probably calculates the maximum number:

  Infinity = 100000000 def S1 (L1, L2, s, i, j, c, scores, cache): key = (I, J, C) if not in the cache: if i == 0 : If c! = '*' And s [0]! = C: v = 0 If J & Gt; = 1 and -Anifinity Other: v = 0 if j> = 0 else -infinity else: v = -L1 for -infinity D: [C] in C2C = Sure = '*' Other L2 + s [I]: jdiff = 1 if s [i]! = C2 and 0 = S1 (L1, L2, S, I-1, J-JDIF, D, score, cache) Score + = Score. Gate (D + C2), 0) V = Max. (V, Score) Cache [Key] = V Return Cache [Key] DRF S (S, Q, Score): L1 = '' .Joined (Sorted [W] Score)) + '*' L2 = '' (Sorted (for W for W [S]) S1 (L1, L2, S + '., Lane (S), Q,' ',' Score ',' Pre> 

There is a few room for optimization:

  • If the calculation is done then the calculation does not end in the beginning
  • When an option is given, then each of L2 Values ​​are tried, whereas only the words that rotate with letters need to be rotated.

Ove Roll, if scoring words are different The algorithm runs in O (QN * ^ 2) time. With the above optimization above, it can be reduced to o (QNW) where w is the number of scoring words.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -