📌  相关文章
📜  尽量减少使两个字符串相等所需的删除或插入

📅  最后修改于: 2021-09-22 10:11:05             🧑  作者: Mango

给定两个字符串ST ,长度NS都是字符串T 的变位,任务是通过执行以下操作最少次数将字符串S转换为T

  • 从任一端删除一个字符。
  • 在任意位置插入一个字符。

打印所需的最少操作次数的计数。

例子:

方法:给定的问题可以通过以下观察来解决:

  • 需要找到A的最长子串的长度,它是B 中的子序列。令该子字符串的长度为L
  • 然后,可以在不干扰现有顺序的情况下插入剩余的字符。
  • 总而言之,最佳答案将等于N – L

因此,从上面观察,需要的操作的最小数量是N和字符串A这是在字符串B的子序列的最长子串的长度之间的差。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the longest substring
// in string A which is a subsequence in B
int findLongestSubstring(
    int posA, int posB, string& A,
    string& B, bool canSkip, int n,
    vector > >& dp)
{
 
    // If the indices are out of bounds
    if (posA >= n || posB >= n) {
        return 0;
    }
 
    // If an already computed subproblem occurred
    if (dp[posA][posB][canSkip] != -1) {
        return dp[posA][posB][canSkip];
    }
 
    // Required answer if the all the
    // characters of A and B are the same
    int op1 = 0;
 
    // Required answer if there is no
    // substring A which is a
    //  subsequence in B
    int op2 = 0;
 
    // Required answer if the current
    // character in B is skipped
    int op3 = 0;
 
    if (A[posA] == B[posB]) {
        op1 = 1 + findLongestSubstring(
                      posA + 1, posB + 1, A,
                      B, 0, n, dp);
    }
    if (canSkip) {
        op2 = findLongestSubstring(
            posA + 1, posB, A, B,
            canSkip, n, dp);
    }
    op3 = findLongestSubstring(
        posA, posB + 1, A, B,
        canSkip, n, dp);
 
    // The answer for the subproblem is
    // the maximum among the three
    return dp[posA][posB][canSkip]
           = max(op1, max(op2, op3));
}
 
// Function to return the minimum strings
// operations required to make two strings equal
void minOperations(string A, string B, int N)
{
    // Initialize the dp vector
    vector > > dp(
        N, vector >(
               N, vector(2, -1)));
 
    cout << N - findLongestSubstring(
                    0, 0, A, B, 1, N, dp);
}
 
// Driver Code
int main()
{
    string A = "abab";
    string B = "baba";
 
    int N = A.size();
 
    minOperations(A, B, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
 
  // Function to find the longest substring
  // in string A which is a subsequence in B
  static int
    findLongestSubstring(int posA, int posB, String A,
                         String B, int canSkip, int n,
                         int dp[][][])
  {
 
    // If the indices are out of bounds
    if (posA >= n || posB >= n)
    {
      return 0;
    }
 
    // If an already computed subproblem occurred
    if (dp[posA][posB][canSkip] != -1)
    {
      return dp[posA][posB][canSkip];
    }
 
    // Required answer if the all the
    // characters of A and B are the same
    int op1 = 0;
 
    // Required answer if there is no
    // substring A which is a
    //  subsequence in B
    int op2 = 0;
 
    // Required answer if the current
    // character in B is skipped
    int op3 = 0;
    if (A.charAt(posA) == B.charAt(posB))
    {
      op1 = 1
        + findLongestSubstring(posA + 1, posB + 1,
                               A, B, 0, n, dp);
    }
    if (canSkip == 1) {
      op2 = findLongestSubstring(posA + 1, posB, A, B,
                                 canSkip, n, dp);
    }
    op3 = findLongestSubstring(posA, posB + 1, A, B,
                               canSkip, n, dp);
 
    // The answer for the subproblem is
    // the maximum among the three
    return dp[posA][posB][canSkip]
      = Math.max(op1, Math.max(op2, op3));
  }
 
  // Function to return the minimum strings
  // operations required to make two strings equal
  static void minOperations(String A, String B, int N)
  {
    // Initialize the dp vector
    int[][][] dp = new int[N][N][2];
 
    for(int i = 0; i < N; i++)
    {
      for(int j = 0; j < N; j++)
      {        
        for(int k = 0; k < 2; k++)
        {    
          dp[i][j][k] = -1;
        }
      }
    }
 
    System.out.println(
      N - findLongestSubstring(0, 0, A, B, 1, N, dp));
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String A = "abab";
    String B = "baba";
    int N = A.length();
    minOperations(A, B, N);
  }
}
 
// This code is contributed by Dharanendra L V


Python3
# Python3 program for the above approach
 
# Function to find the longest substring
# in A which is a subsequence in B
def findLongestSubstring(posA, posB, A, B, canSkip, n):
    global dp
    if (posA >= n or posB >= n):
        return 0
 
    # If an already computed subproblem occurred
    if (dp[posA][posB][canSkip] != -1):
        return dp[posA][posB][canSkip]
 
    # Required answer if the all the
    # characters of A and B are the same
    op1 = 0
 
    # Required answer if there is no
    # subA which is a
    # subsequence in B
    op2 = 0
 
    # Required answer if the current
    # character in B is skipped
    op3 = 0
 
    if (A[posA] == B[posB]):
        op1 = 1 + findLongestSubstring(posA + 1, posB + 1, A, B, 0, n)
    if (canSkip):
        op2 = findLongestSubstring(posA + 1, posB, A, B, canSkip, n)
 
    op3 = findLongestSubstring(posA, posB + 1, A, B, canSkip, n)
 
    # The answer for the subproblem is
    # the maximum among the three
    dp[posA][posB][canSkip] = max(op1, max(op2, op3))
    return dp[posA][posB][canSkip]
 
# Function to return the minimum strings
# operations required to make two strings equal
def minOperations(A, B, N):
    print(N - findLongestSubstring(0, 0, A, B, 1, N))
 
# Driver Code
if __name__ == '__main__':
    A = "abab"
    B = "baba"
    dp = [[[-1, -1] for i in range(len(A))] for i in range(len(A))]
    N = len(A)
    minOperations(A, B, N)
     
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG
{
 
      // Function to find the longest substring
    // in string A which is a subsequence in B
    static int
    findLongestSubstring(int posA, int posB, String A,
                         String B, int canSkip, int n,
                         int[, , ] dp)
    {
 
        // If the indices are out of bounds
        if (posA >= n || posB >= n) {
            return 0;
        }
 
        // If an already computed subproblem occurred
        if (dp[posA, posB, canSkip] != -1) {
            return dp[posA, posB, canSkip];
        }
 
        // Required answer if the all the
        // characters of A and B are the same
        int op1 = 0;
 
        // Required answer if there is no
        // substring A which is a
        //  subsequence in B
        int op2 = 0;
 
        // Required answer if the current
        // character in B is skipped
        int op3 = 0;
 
        if (A[posA] == B[posB]) {
            op1 = 1
                  + findLongestSubstring(posA + 1, posB + 1,
                                         A, B, 0, n, dp);
        }
        if (canSkip == 1) {
            op2 = findLongestSubstring(posA + 1, posB, A, B,
                                       canSkip, n, dp);
        }
        op3 = findLongestSubstring(posA, posB + 1, A, B,
                                   canSkip, n, dp);
 
        // The answer for the subproblem is
        // the maximum among the three
        return dp[posA, posB, canSkip]
            = Math.Max(op1, Math.Max(op2, op3));
    }
 
    // Function to return the minimum strings
    // operations required to make two strings equal
    static void minOperations(String A, String B, int N)
    {
        // Initialize the dp vector
        int[, , ] dp = new int[N, N, 2];
           
          for(int i = 0; i < N; i++)
        {      
              for(int j = 0; j < N; j++)
            {               
                  for(int k = 0; k < 2; k++)
                {    
                      dp[i, j, k] = -1;
                }
            }
        }
        Console.WriteLine(
            N - findLongestSubstring(0, 0, A, B, 1, N, dp));
    }
 
    // Driver Code
    static public void Main ()
    {
        String A = "abab";
        String B = "baba";
        int N = A.Length;
        minOperations(A, B, N);
    }
}
 
// This code is contributed by Dharanendra L V


Javascript


输出:
1

时间复杂度: O(N 2 )
空间复杂度: O(N 2 )

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程