📜  通过分别从字符串A 和 B 中选择子字符串 X 和 Y 来最大化 [length(X)/2^(XOR(X, Y))]

📅  最后修改于: 2021-09-17 07:45:06             🧑  作者: Mango

给定两个大小分别为NM 的二进制字符串AB ,任务是通过从给定字符串选择两个相等长度的子字符串XY来最大化(X) / 2 XOR(X, Y)的长度值分别为AB。

例子:

方法:给定的问题可以通过观察需要最大化的表达式来解决,因此分母必须最小,并且为了最小化子串XY的Bitwise XOR的值必须最小,即为零并且使Bitwise XOR 的值为零,两个子串必须相同。因此,问题简化为找到字符串AB的最长公共子串。请按照以下步骤解决问题:

  • 初始化一个二维数组,比如LCSuff[M + 1][N + 1]来存储子串的最长公共后缀的长度。
  • 初始化一个变量,比如result0来存储给定表达式的结果最大值。
  • 遍历范围[0,M],用变量i和嵌套迭代范围[0,N],用变量j,并执行以下步骤:
    • 如果i等于0j等于0 ,则更新LCSSuff[i][j] 的值等于0
    • 否则,如果A[i – 1] 的值等于A[j – 1]则将LCSSuff[i][j]的值更新为LCSSuff[i – 1][j – 1] + 1并更新结果作为resultLCSSuff[i][j]最大值
    • 否则,将LCSSuff[i][j]的值更新为0
  • 完成以上步骤后,打印result的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the length of the
// longest common substring of the
// string X and Y
int LCSubStr(char* A, char* B, int m, int n)
{
    // LCSuff[i][j] stores the lengths
    // of the longest common suffixes
    // of substrings
    int LCSuff[m + 1][n + 1];
    int result = 0;
 
    // Itearate over strings A and B
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
 
            // If first row or column
            if (i == 0 || j == 0)
                LCSuff[i][j] = 0;
 
            // If matching is found
            else if (A[i - 1] == B[j - 1]) {
                LCSuff[i][j]
                    = LCSuff[i - 1][j - 1]
                      + 1;
                result = max(result,
                             LCSuff[i][j]);
            }
 
            // Otherwise, if matching
            // is not found
            else
                LCSuff[i][j] = 0;
        }
    }
 
    // Finally, return the resultant
    // maximum value LCS
    return result;
}
 
// Driver Code
int main()
{
    char A[] = "0110";
    char B[] = "1101";
    int M = strlen(A);
    int N = strlen(B);
 
    // Function Call
    cout << LCSubStr(A, B, M, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the length of the
// longest common substring of the
// string X and Y
static int lcsubtr(char a[], char b[], int length1,
                   int length2)
{
     
    // LCSuff[i][j] stores the lengths
    // of the longest common suffixes
    // of substrings
    int dp[][] = new int[length1 + 1][length2 + 1];
    int max = 0;
     
    // Itearate over strings A and B
    for(int i = 0; i <= length1; ++i)
    {
        for(int j = 0; j <= length2; ++j)
        {
             
            // If first row or column
            if (i == 0 || j == 0)
            {
                dp[i][j] = 0;
            }
             
            // If matching is found
            else if (a[i - 1] == b[j - 1])
            {
                dp[i][j] = dp[i - 1][j - 1] + 1;
                max = Math.max(dp[i][j], max);
            }
             
            // Otherwise, if matching
            // is not found
            else
            {
                dp[i][j] = 0;
            }
        }
    }
     
    // Finally, return the resultant
    // maximum value LCS
    return max;
}
 
// Driver Code
public static void main(String[] args)
{
    String m = "0110";
    String n = "1101";
    char m1[] = m.toCharArray();
    char m2[] = n.toCharArray();
     
    // Function Call
    System.out.println(lcsubtr(m1, m2, m1.length,
                                       m2.length));
}
}
 
// This code is contributed by zack_aayush


Python3
# Python 3 program for the above approach
 
# Function to find the length of the
# longest common substring of the
# string X and Y
def LCSubStr(A, B, m, n):
   
    # LCSuff[i][j] stores the lengths
    # of the longest common suffixes
    # of substrings
    LCSuff = [[0 for i in range(n+1)] for j in range(m+1)]
    result = 0
 
    # Itearate over strings A and B
    for i in range(m + 1):
        for j in range(n + 1):
           
            # If first row or column
            if (i == 0 or j == 0):
                LCSuff[i][j] = 0
 
            # If matching is found
            elif(A[i - 1] == B[j - 1]):
                LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1
                result = max(result,LCSuff[i][j])
 
            # Otherwise, if matching
            # is not found
            else:
                LCSuff[i][j] = 0
 
    # Finally, return the resultant
    # maximum value LCS
    return result
 
# Driver Code
if __name__ == '__main__':
    A = "0110"
    B = "1101"
    M = len(A)
    N = len(B)
 
    # Function Call
    print(LCSubStr(A, B, M, N))
 
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the length of the
// longest common substring of the
// string X and Y
static int lcsubtr(char[] a, char[] b, int length1,
                   int length2)
{
     
    // LCSuff[i][j] stores the lengths
    // of the longest common suffixes
    // of substings
    int[,] dp = new int[length1 + 1, length2 + 1];
    int max = 0;
     
    // Itearate over strings A and B
    for(int i = 0; i <= length1; ++i)
    {
        for(int j = 0; j <= length2; ++j)
        {
             
            // If first row or column
            if (i == 0 || j == 0)
            {
                dp[i, j] = 0;
            }
             
            // If matching is found
            else if (a[i - 1] == b[j - 1])
            {
                dp[i, j] = dp[i - 1, j - 1] + 1;
                max = Math.Max(dp[i, j], max);
            }
             
            // Otherwise, if matching
            // is not found
            else
            {
                dp[i, j] = 0;
            }
        }
    }
     
    // Finally, return the resultant
    // maximum value LCS
    return max;
}
 
// Driver Code
public static void Main()
{
    string m = "0110";
    string n = "1101";
    char[] m1 = m.ToCharArray();
    char[] m2 = n.ToCharArray();
     
    // Function Call
    Console.Write(lcsubtr(m1, m2, m1.Length,
                                       m2.Length));
}
}
 
// This code is contributed by target_2.


Javascript


输出:
3

时间复杂度: O(M*N)
辅助空间: O(M*N)

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