📌  相关文章
📜  通过从给定的二进制字符串中选择相等长度的子字符串来最大化给定的函数

📅  最后修改于: 2022-05-13 01:56:10.200000             🧑  作者: Mango

通过从给定的二进制字符串中选择相等长度的子字符串来最大化给定的函数

给定两个二进制字符串s1s2 。任务是从s1s2中选择子串,例如sub1sub2的长度相等,以使函数最大化:

例子:

方法:为了最大化给定函数,需要选择具有最小 XOR 的大子字符串。要最小化分母,请选择子串,使sub1sub2的 XOR 始终为0 ,以便分母项始终为1 (2 0 )。因此,为此,从两个字符串s1s2中找到最长的公共子字符串,并打印它的长度,这将是所需的答案。

以下是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
int dp[1000][1000];
 
// Function to find longest common substring.
int lcs(string s, string k, int n, int m)
{
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            if (i == 0 or j == 0) {
                dp[i][j] = 0;
            }
            else if (s[i - 1] == k[j - 1]) {
                dp[i][j] = 1 + dp[i - 1][j - 1];
            }
            else {
                dp[i][j] = max(dp[i - 1][j],
                               dp[i][j - 1]);
            }
        }
    }
 
    // Return the result
    return dp[n][m];
}
 
// Driver Code
int main()
{
    string s1 = "1110";
    string s2 = "1101";
 
    cout << lcs(s1, s2,
                s1.size(), s2.size());
 
    return 0;
}


Java
// Java program for above approach
class GFG{
   
  static int dp[][] = new int[1000][1000];
 
  // Function to find longest common substring.
  static int lcs(String s, String k, int n, int m)
  {
      for (int i = 0; i <= n; i++) {
          for (int j = 0; j <= m; j++) {
              if (i == 0 || j == 0) {
                  dp[i][j] = 0;
              }
              else if (s.charAt(i - 1) == k.charAt(j - 1)) {
                  dp[i][j] = 1 + dp[i - 1][j - 1];
              }
              else {
                  dp[i][j] = Math.max(dp[i - 1][j],
                                 dp[i][j - 1]);
              }
          }
      }
 
      // Return the result
      return dp[n][m];
  }
 
  // Driver Code
  public static void main(String [] args)
  {
      String s1 = "1110";
      String s2 = "1101";
 
      System.out.print(lcs(s1, s2,
                  s1.length(), s2.length()));
 
       
  }
}
 
// This code is contributed by AR_Gaurav


Python3
# Python3 program for above approach
 
import numpy as np;
 
dp = np.zeros((1000,1000));
 
# Function to find longest common substring.
def lcs( s,  k,  n,  m) :
 
    for i in range(n + 1) :
        for j in range(m + 1) :
            if (i == 0 or j == 0) :
                dp[i][j] = 0;
             
            elif (s[i - 1] == k[j - 1]) :
                dp[i][j] = 1 + dp[i - 1][j - 1];
             
            else :
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
 
    # Return the result
    return dp[n][m];
 
# Driver Code
if __name__ == "__main__" :
 
    s1 = "1110";
    s2 = "1101";
 
    print(lcs(s1, s2,len(s1), len(s2)));
 
   # This code is contributed by AnkThon


C#
// C# program for above approach
using System;
public class GFG{
   
  static int [,]dp = new int[1000,1000];
 
  // Function to find longest common substring.
  static int lcs(string s, string k, int n, int m)
  {
      for (int i = 0; i <= n; i++) {
          for (int j = 0; j <= m; j++) {
              if (i == 0 || j == 0) {
                  dp[i, j] = 0;
              }
              else if (s[i - 1] == k[j - 1]) {
                  dp[i, j] = 1 + dp[i - 1, j - 1];
              }
              else {
                  dp[i, j] = Math.Max(dp[i - 1, j],
                                 dp[i, j - 1]);
              }
          }
      }
 
      // Return the result
      return dp[n, m];
  }
 
  // Driver Code
  public static void Main(string [] args)
  {
      string s1 = "1110";
      string s2 = "1101";
 
      Console.Write(lcs(s1, s2, s1.Length, s2.Length));
  }
}
 
// This code is contributed by AnkThon


Javascript


输出
3

时间复杂度: O(N*M),其中 N 是 s1 的大小,M 是 s2 的大小。

辅助空间: O(N*M),其中 N 是 s1 的大小,M 是 s2 的大小。