📜  使两个数字字符串相同的最低成本

📅  最后修改于: 2021-05-06 09:21:19             🧑  作者: Mango

给定两个数字字符串A和B。数字字符串是仅包含数字[‘0’-‘9’]的字符串。

任务是使两个字符串的最小成本相等。唯一允许您执行的操作是从任何字符串(A或B)中删除字符(即数字)。删除数字D的成本为D个单位。

例子

该问题是流行的动态编程问题的变体-最长公共子序列。这个想法是找到最大权重的公共子序列,这将是我们所需的最佳相同字符串。要找到删除的代价,请从字符串A和B的总和中减去最大权重公共子序列的总和。

下面是上述想法的实现:

C++
// CPP program to find minimum cost to make
// two numeric strings identical
  
#include 
  
using namespace std;
  
typedef long long int ll;
  
// Function to find weight of LCS
int lcs(int dp[101][101], string a, string b,
        int m, int n)
{
    for (int i = 0; i < 100; i++)
        for (int j = 0; j < 100; j++)
            dp[i][j] = -1;
  
    if (m < 0 || n < 0) {
        return 0;
    }
  
    // if this state is already
    // calculated then return
    if (dp[m][n] != -1)
        return dp[m][n];
  
    int ans = 0;
    if (a[m] == b[n]) {
        // adding required weight for
        // particular match
        ans = int(a[m] - 48) + lcs(dp, a, b,
                                   m - 1, n - 1);
    }
    else
        // recurse for left and right child
        // and store the max
        ans = max(lcs(dp, a, b, m - 1, n),
                  lcs(dp, a, b, m, n - 1));
  
    dp[m][n] = ans;
    return ans;
}
  
// Function to calculate cost of string
int costOfString(string str)
{
    int cost = 0;
  
    for (int i = 0; i < str.length(); i++)
        cost += int(str[i] - 48);
  
    return cost;
}
  
// Driver code
int main()
{
    string a, b;
  
    a = "9142";
    b = "1429";
  
    int dp[101][101];
  
    // Minimum cost needed to make two strings identical
    cout << (costOfString(a) + costOfString(b) - 
                       2 * lcs(dp, a, b, a.length() - 1, 
                                       b.length() - 1));
  
    return 0;
}


Java
// Java program to find minimum cost to make
// two numeric strings identical
  
import java.io.*;
  
class GFG {
   
  
// Function to find weight of LCS
 static int lcs(int dp[][], String a, String b,
        int m, int n)
{
    for (int i = 0; i < 100; i++)
        for (int j = 0; j < 100; j++)
            dp[i][j] = -1;
  
    if (m < 0 || n < 0) {
        return 0;
    }
  
    // if this state is already
    // calculated then return
    if (dp[m][n] != -1)
        return dp[m][n];
  
    int ans = 0;
    if (a.charAt(m) == b.charAt(n)) {
        // adding required weight for
        // particular match
        ans = (a.charAt(m) - 48) + lcs(dp, a, b,
                                m - 1, n - 1);
    }
    else
        // recurse for left and right child
        // and store the max
        ans = Math.max(lcs(dp, a, b, m - 1, n),
                lcs(dp, a, b, m, n - 1));
  
    dp[m][n] = ans;
    return ans;
}
  
// Function to calculate cost of string
 static int costOfString(String str)
{
    int cost = 0;
  
    for (int i = 0; i < str.length(); i++)
        cost += (str.charAt(i) - 48);
  
    return cost;
}
  
// Driver code
    public static void main (String[] args) {
            String a, b;
  
    a = "9142";
    b = "1429";
  
    int dp[][] = new int[101][101];
  
    // Minimum cost needed to make two strings identical
    System.out.print( (costOfString(a) + costOfString(b) - 
                    2 * lcs(dp, a, b, a.length() - 1, 
                                    b.length() - 1)));
  
    }
}
// This code is contributed by anuj_67.


Python 3
# Python 3 program to find minimum cost 
# to make two numeric strings identical
  
# Function to find weight of LCS
def lcs(dp, a, b, m, n):
  
    for i in range(100):
        for j in range(100):
            dp[i][j] = -1
  
    if (m < 0 or n < 0) :
        return 0
  
    # if this state is already calculated 
    # then return
    if (dp[m][n] != -1):
        return dp[m][n]
  
    ans = 0
    if (a[m] == b[n]):
          
        # adding required weight for
        # particular match
        ans = (ord(a[m]) - 48) + lcs(dp, a, b,
                                     m - 1, n - 1)
      
    else:
          
        # recurse for left and right child
        # and store the max
        ans = max(lcs(dp, a, b, m - 1, n),
                  lcs(dp, a, b, m, n - 1))
  
    dp[m][n] = ans
    return ans
  
# Function to calculate cost of string
def costOfString(s):
      
    cost = 0
  
    for i in range(len(s)):
        cost += (ord(s[i]) - 48)
  
    return cost
  
# Driver code
if __name__ == "__main__":
  
    a = "9142"
    b = "1429"
  
    dp = [[0 for x in range(101)] 
             for y in range(101)]
  
    # Minimum cost needed to make two
    # strings identical
    print(costOfString(a) + costOfString(b) - 2 * 
           lcs(dp, a, b, len(a) - 1, len(b) - 1))
  
# This code is contributed by ita_c


C#
// C# program to find minimum cost to make 
// two numeric strings identical 
using System;
public class GFG { 
  
// Function to find weight of LCS 
static int lcs(int [,]dp, String a, String b, 
        int m, int n) 
{ 
    for (int i = 0; i < 100; i++) 
        for (int j = 0; j < 100; j++) 
            dp[i,j] = -1; 
  
    if (m < 0 || n < 0) { 
        return 0; 
    } 
  
    // if this state is already 
    // calculated then return 
    if (dp[m,n] != -1) 
        return dp[m,n]; 
  
    int ans = 0; 
    if (a[m] == b[n]) { 
        // adding required weight for 
        // particular match 
        ans = (a[m] - 48) + lcs(dp, a, b, m - 1, n - 1); 
    } 
    else
        // recurse for left and right child 
        // and store the max 
        ans = Math.Max(lcs(dp, a, b, m - 1, n), 
                lcs(dp, a, b, m, n - 1)); 
  
    dp[m,n] = ans; 
    return ans; 
} 
  
// Function to calculate cost of string 
static int costOfString(String str) 
{ 
    int cost = 0; 
  
    for (int i = 0; i < str.Length; i++) 
        cost += (str[i] - 48); 
  
    return cost; 
} 
  
// Driver code 
    public static void Main () { 
            String a, b; 
  
    a = "9142"; 
    b = "1429"; 
  
    int [,]dp = new int[101,101]; 
  
    // Minimum cost needed to make two strings identical 
    Console.Write( (costOfString(a) + costOfString(b) - 
                2 * lcs(dp, a, b, a.Length- 1, 
                b.Length - 1))); 
  
    } 
} 
// This code is contributed by Rajput-Ji


输出:
14