String Alignment Score
InterviewTime limit2sMemory limit128 MB
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.
- If the two characters are the same lowercase letter, you earn A points.
- If exactly one of the two characters is a space, you earn B points.
- 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.
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.