📜  打印最短的常见超序列

📅  最后修改于: 2021-04-24 05:11:44             🧑  作者: Mango

给定两个字符串X和Y,打印同时包含X和Y作为子序列的最短字符串。如果存在多个最短超序列,请打印其中的任何一个。

例子:

Input: X = "AGGTAB",  Y = "GXTXAYB"
Output: "AGXGTXAYB" OR "AGGXTXAYB" 
OR Any string that represents shortest
supersequence of X and Y

Input: X = "HELLO",  Y = "GEEK"
Output: "GEHEKLLO" OR "GHEEKLLO"
OR Any string that represents shortest 
supersequence of X and Y

我们在这里讨论了如何打印两个给定字符串的最短可能超序列长度。在这篇文章中,我们将打印最短的超序列。

我们已经在下面的算法中进行了讨论,以找到先前后

Let X[0..m-1] and Y[0..n-1] be two strings and m and be respective 
lengths.

if (m == 0) return n;
if (n == 0) return m;

// If last characters are same, then add 1 to result and
// recur for X[]
if (X[m-1] == Y[n-1]) 
    return 1 + SCS(X, Y, m-1, n-1);

// Else find shortest of following two
//  a) Remove last character from X and recur
//  b) Remove last character from Y and recur
else return 1 + min( SCS(X, Y, m-1, n), SCS(X, Y, m, n-1) );

下表显示了如果我们使用动态编程以自底向上的方式对字符串X =“ AGGTAB”和Y =“ GXTXAYB”进行求解,则上述算法所遵循的步骤。

最短超序列问题DP表

使用DP解决方案矩阵,我们可以按照以下步骤轻松打印两个字符串的最短超序列–

We start from the bottom-right most cell of the matrix and 
push characters in output string based on below rules-

 1. If the characters corresponding to current cell (i, j) 
    in X and Y are same, then the character is part of shortest 
    supersequence. We append it in output string and move 
    diagonally to next cell (i.e. (i - 1, j - 1)).

 2. If the characters corresponding to current cell (i, j)
    in X and Y are different, we have two choices -

    If matrix[i - 1][j] > matrix[i][j - 1],
    we add character corresponding to current 
    cell (i, j) in string Y in output string 
    and move to the left cell i.e. (i, j - 1)
    else
    we add character corresponding to current 
    cell (i, j) in string X in output string 
    and move to the top cell i.e. (i - 1, j)

 3. If string Y reaches its end i.e. j = 0, we add remaining
    characters of string X in the output string
    else if string X reaches its end i.e. i = 0, we add 
    remaining characters of string Y in the output string.

以下是上述想法的实现–

C++
/* A dynamic programming based C++ program print
   shortest supersequence of two strings */
#include 
using namespace std;
  
// returns shortest supersequence of X and Y
string printShortestSuperSeq(string X, string Y)
{
    int m = X.length();
    int n = Y.length();
  
    // dp[i][j] contains length of shortest supersequence
    // for X[0..i-1] and Y[0..j-1]
    int dp[m + 1][n + 1];
  
    // Fill table in bottom up manner
    for (int i = 0; i <= m; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            // Below steps follow recurrence relation
            if(i == 0)
                dp[i][j] = j;
            else if(j == 0)
                dp[i][j] = i;
            else if(X[i - 1] == Y[j - 1])
                dp[i][j] = 1 + dp[i - 1][j - 1];
            else
                dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1]);
        }
    }
  
    // Following code is used to print shortest supersequence
  
    // dp[m][n] stores the length of the shortest supersequence
    // of X and Y
    int index = dp[m][n];
  
    // string to store the shortest supersequence
    string str;
  
    // Start from the bottom right corner and one by one
    // push characters in output string
    int i = m, j = n;
    while (i > 0 && j > 0)
    {
        // If current character in X and Y are same, then
        // current character is part of shortest supersequence
        if (X[i - 1] == Y[j - 1])
        {
            // Put current character in result
            str.push_back(X[i - 1]);
  
            // reduce values of i, j and index
            i--, j--, index--;
        }
  
        // If current character in X and Y are different
        else if (dp[i - 1][j] > dp[i][j - 1])
        {
            // Put current character of Y in result
            str.push_back(Y[j - 1]);
  
            // reduce values of j and index
            j--, index--;
        }
        else
        {
            // Put current character of X in result
            str.push_back(X[i - 1]);
  
            // reduce values of i and index
            i--, index--;
        }
    }
  
    // If Y reaches its end, put remaining characters
    // of X in the result string
    while (i > 0)
    {
        str.push_back(X[i - 1]);
        i--, index--;
    }
  
    // If X reaches its end, put remaining characters
    // of Y in the result string
    while (j > 0)
    {
        str.push_back(Y[j - 1]);
        j--, index--;
    }
  
    // reverse the string and return it
    reverse(str.begin(), str.end());
    return str;
}
  
// Driver program to test above function
int main()
{
    string X = "AGGTAB";
    string Y = "GXTXAYB";
  
    cstr << printShortestSuperSeq(X, Y);
  
    return 0;
}


Java
/* A dynamic programming based Java program print 
shortest supersequence of two strings */
class GFG {
  
    // returns shortest supersequence of X and Y 
    static String printShortestSuperSeq(String X, String Y)
    {
        int m = X.length();
        int n = Y.length();
  
        // dp[i][j] contains length of 
        // shortest supersequence 
        // for X[0..i-1] and Y[0..j-1] 
        int dp[][] = new int[m + 1][n + 1];
  
        // Fill table in bottom up manner 
        for (int i = 0; i <= m; i++) 
        {
            for (int j = 0; j <= n; j++) 
            {
                  
                // Below steps follow recurrence relation 
                if (i == 0) 
                {
                    dp[i][j] = j;
                } 
                else if (j == 0) 
                {
                    dp[i][j] = i;
                } 
                else if (X.charAt(i - 1) == Y.charAt(j - 1)) 
                {
                    dp[i][j] = 1 + dp[i - 1][j - 1];
                }
                else 
                {
                    dp[i][j] = 1 + Math.min(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
  
        // Following code is used to print 
        // shortest supersequence dp[m][n] s
        // tores the length of the shortest
        // supersequence of X and Y 
        int index = dp[m][n];
  
        // string to store the shortest supersequence 
        String str = "";
  
        // Start from the bottom right corner and one by one 
        // push characters in output string 
        int i = m, j = n;
        while (i > 0 && j > 0)
          
        {
            // If current character in X and Y are same, then 
            // current character is part of shortest supersequence 
            if (X.charAt(i - 1) == Y.charAt(j - 1)) 
              
            {
                // Put current character in result 
                str += (X.charAt(i - 1));
  
                // reduce values of i, j and index 
                i--;
                j--;
                index--;
            } 
              
            // If current character in X and Y are different 
            else if (dp[i - 1][j] > dp[i][j - 1])
            {
                  
                // Put current character of Y in result 
                str += (Y.charAt(j - 1));
  
                // reduce values of j and index 
                j--;
                index--;
            } 
            else 
            {
                  
                // Put current character of X in result 
                str += (X.charAt(i - 1));
  
                // reduce values of i and index 
                i--;
                index--;
            }
        }
  
        // If Y reaches its end, put remaining characters 
        // of X in the result string 
        while (i > 0) 
        {
            str += (X.charAt(i - 1));
            i--;
            index--;
        }
  
        // If X reaches its end, put remaining characters 
        // of Y in the result string 
        while (j > 0)
        {
            str += (Y.charAt(j - 1));
            j--;
            index--;
        }
  
        // reverse the string and return it 
        str = reverse(str);
        return str;
    }
  
    static String reverse(String input) 
    {
        char[] temparray = input.toCharArray();
        int left, right = 0;
        right = temparray.length - 1;
  
        for (left = 0; left < right; left++, right--)
        {
            // Swap values of left and right 
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return String.valueOf(temparray);
    }
      
    // Driver code
    public static void main(String[] args) 
    {
        String X = "AGGTAB";
        String Y = "GXTXAYB";
        System.out.println(printShortestSuperSeq(X, Y));
    }
} 
  
// This code is contributed by 29AjayKumar


Python3
# A dynamic programming based Python3 program print
# shortest supersequence of two strings
  
# returns shortest supersequence of X and Y
def printShortestSuperSeq(x, y):
    m = len(x)
    n = len(y)
  
    # dp[i][j] contains length of shortest 
    # supersequence for X[0..i-1] and Y[0..j-1]
    dp = [[0 for i in range(n + 1)] 
             for j in range(n + 1)]
  
    # Fill table in bottom up manner
    for i in range(m + 1):
        for j in range(n + 1):
  
            # Below steps follow recurrence relation
            if i == 0:
                dp[i][j] = j
            elif j == 0:
                dp[i][j] = i
            elif x[i - 1] == y[j - 1]:
                dp[i][j] = 1 + dp[i - 1][j - 1]
            else:
                dp[i][j] = 1 + min(dp[i - 1][j], 
                                   dp[i][j - 1])
  
    # Following code is used to print
    # shortest supersequence
  
    # dp[m][n] stores the length of the 
    # shortest supersequence of X and Y
    index = dp[m][n]
  
    # string to store the shortest supersequence
    string = ""
  
    # Start from the bottom right corner and 
    # one by one push characters in output string
    i = m
    j = n
    while i > 0 and j > 0:
  
        # If current character in X and Y are same, 
        # then current character is part of
        # shortest supersequence
        if x[i - 1] == y[j - 1]:
  
            # Put current character in result
            string += x[i - 1]
  
            # reduce values of i, j and index
            i -= 1
            j -= 1
            index -= 1
  
        # If current character in X and Y are different
        elif dp[i - 1][j] > dp[i][j - 1]:
  
            # Put current character of Y in result
            string += y[j - 1]
  
            # reduce values of j and index
            j -= 1
            index -= 1
        else:
  
            # Put current character of X in result
            string += x[i - 1]
  
            # reduce values of i and index
            i -= 1
            index -= 1
  
    # If Y reaches its end, put remaining characters
    # of X in the result string
    while i > 0:
        string += x[i - 1]
        i -= 1
        index -= 1
  
    # If X reaches its end, put remaining characters
    # of Y in the result string
    while j > 0:
        string += y[j - 1]
        j -= 1
        index -= 1
  
    string = list(string)
  
    # reverse the string and return it
    string.reverse()
    return ''.join(string)
  
# Driver Code
if __name__ == "__main__":
    x = "AGGTAB"
    y = "GXTXAYB"
  
    print(printShortestSuperSeq(x, y))
  
# This code is contributed by
# sanjeev2552


C#
/* A dynamic programming based C# program print 
shortest supersequence of two strings */
using System;
  
class GFG
{
  
    // returns shortest supersequence of X and Y 
    static String printShortestSuperSeq(String X, String Y)
    {
        int m = X.Length;
        int n = Y.Length;
  
        // dp[i,j] contains length of 
        // shortest supersequence 
        // for X[0..i-1] and Y[0..j-1] 
        int [,]dp = new int[m + 1, n + 1];
        int i, j;
          
        // Fill table in bottom up manner 
        for (i = 0; i <= m; i++) 
        {
            for (j = 0; j <= n; j++) 
            {
                  
                // Below steps follow recurrence relation 
                if (i == 0) 
                {
                    dp[i, j] = j;
                } 
                else if (j == 0) 
                {
                    dp[i, j] = i;
                } 
                else if (X[i - 1] == Y[j - 1]) 
                {
                    dp[i, j] = 1 + dp[i - 1, j - 1];
                }
                else
                {
                    dp[i, j] = 1 + Math.Min(dp[i - 1, j], dp[i, j - 1]);
                }
            }
        }
  
        // Following code is used to print 
        // shortest supersequence dp[m,n] s
        // tores the length of the shortest
        // supersequence of X and Y 
        int index = dp[m, n];
  
        // string to store the shortest supersequence 
        String str = "";
  
        // Start from the bottom right corner and one by one 
        // push characters in output string 
        i = m; j = n;
        while (i > 0 && j > 0)
          
        {
            // If current character in X and Y are same, then 
            // current character is part of shortest supersequence 
            if (X[i - 1] == Y[j - 1]) 
              
            {
                // Put current character in result 
                str += (X[i - 1]);
  
                // reduce values of i, j and index 
                i--;
                j--;
                index--;
            } 
              
            // If current character in X and Y are different 
            else if (dp[i - 1, j] > dp[i, j - 1])
            {
                  
                // Put current character of Y in result 
                str += (Y[j - 1]);
  
                // reduce values of j and index 
                j--;
                index--;
            } 
            else
            {
                  
                // Put current character of X in result 
                str += (X[i - 1]);
  
                // reduce values of i and index 
                i--;
                index--;
            }
        }
  
        // If Y reaches its end, put remaining characters 
        // of X in the result string 
        while (i > 0) 
        {
            str += (X[i - 1]);
            i--;
            index--;
        }
  
        // If X reaches its end, put remaining characters 
        // of Y in the result string 
        while (j > 0)
        {
            str += (Y[j - 1]);
            j--;
            index--;
        }
  
        // reverse the string and return it 
        str = reverse(str);
        return str;
    }
  
    static String reverse(String input) 
    {
        char[] temparray = input.ToCharArray();
        int left, right = 0;
        right = temparray.Length - 1;
  
        for (left = 0; left < right; left++, right--)
        {
            // Swap values of left and right 
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return String.Join("",temparray);
    }
      
    // Driver code
    public static void Main(String[] args) 
    {
        String X = "AGGTAB";
        String Y = "GXTXAYB";
        Console.WriteLine(printShortestSuperSeq(X, Y));
    }
}
  
/* This code has been contributed 
by PrinciRaj1992*/


输出:

AGXGTXAYB

上述解决方案的时间复杂度为O(n 2 )。
该程序使用的辅助空间为O(n 2 )。