Shortest Supersequences X40399


Statement
 

pdf   zip

Find shortest supersequences of pairs of strings. A supersequence of a string ss is a string tt such that ss is obtained from tt by deleting zero or more characters. Given two strings ss and tt, of course the concatenation stst is a supersequence of both; often, however, there are shorter ones. For instance, ’AGGXTXAYB’ is a shortest supersequence of ’AGGTAB’ and ’GXTXAYB’, while ’blueed’ and ’bleued’ are both shortest supersequences of ’bleed’ and ’blue’, and ’bacfkorward’ is one of the shortest supersequences of ’backward’ and ’forward’.

Input

Input is a sequence of cases. A case consists of two words ss and tt in the same line; both consist only of letters. Each line brings exactly one case.

Output

For each case, print the length of the shortest supersequences of both strings.

Observation

This is a classical example where one must apply Dynamic Programming. Note that there may be several different shortest supersequences; however, all must have the same length. The automatic correction will not want to see the shortest supersequences themselves: it only expects their length. However, along your programming and debugging task, it will be helpful to be able to write down for human eyes not only the length but also at least one of these shortest supersequences. Which one will you write? Are you able to construct your program so as to identify the alphabetically smallest shortest supersequence?

Public test cases
  • Input

    forward backward
    blinding lights
    hello geek
    bleed blue
    ACTCAAG TCCAGA 
    AGGTAB GXTXAYB

    Output

    11
    11
    8
    6
    9
    9
    
  • Information
    Author
    José Luis Balcázar
    Language
    English
    Official solutions
    Python
    User solutions
    Python