Under construction

Still being built. Things may change

String Alignment Score

Interview

Time limit2sMemory limit128 MB

Summary
Compute the maximum scoring alignment of two strings using gap and mismatch/match scoring, which is a classic sequence alignment (edit distance style) DP.
Level

Medium4 of 10

Topics
Dynamic programming, String
Solved
No attempts yet

Problem

You are given two nonempty strings X and Y consisting of lowercase English letters. You may insert spaces anywhere between characters or at either end of each string. After inserting spaces, the two strings must have the same length, and no position may contain spaces in both strings.

For each aligned position, you earn points as follows.

  1. If the two characters are the same lowercase letter, you earn A points.
  2. If exactly one of the two characters is a space, you earn B points.
  3. If both characters are lowercase letters and they are different, you earn C points.

When A = 10, B = -1, and C = -5, one possible alignment is shown below.

Position1234
Xa(space)bc
Y(space)d(space)c

The scores at the four positions are -1, -1, -1, and 10, so the total score is 7.

Given X and Y, find the maximum total score obtainable by inserting spaces.

Input

The first line contains three integers A, B, and C. They satisfy 0 < A <= 10,000 and -10,000 <= B, C < 0.

The second line contains X, and the third line contains Y. Both strings consist only of lowercase English letters, and each has length between 1 and 3,000 inclusive.

Output

Print the maximum total score.

Examples1

  1. Example 1

    Input
    10 -1 -5
    abc
    dc
    
    Expected output
    7