Problem 460
Shortest Common Supersequence Length
Medium · 20 pts · String, Dynamic Programming
Problem
A supersequence of two strings is a string that contains both of them as subsequences (each string's characters appear in it in order, possibly with other characters in between). Find the length of the shortest common supersequence of A and B. Characters shared through a common subsequence only need to be written once, so the answer is |A| + |B| - LCS(A, B).
Input
Two lines: strings A and B of lowercase letters.
Output
Print the length of the shortest common supersequence.
Constraints
1 <= |A|, |B| <= 1000
Example input
abac cab
Example output
5
Explanation
'abac' and 'cab' share the subsequence 'ab', so the shortest supersequence has length 4 + 3 - 2 = 5, for example 'cabac'.