给定一个数组arr[] ,任务是找到具有给定 OR 值M的最长子序列。如果没有这样的子序列,则打印0 。
例子:
Input: arr[] = {3, 7, 2, 3}, M = 3
Output: 3
{3, 2, 3} is the required subsequence
3 | 2 | 3 = 3
Input: arr[] = {2, 2}, M = 3
Output: 0
方法:一个简单的解决方案是生成所有可能的子序列,然后找到其中具有所需 OR 值的最大子序列。然而,对于较小的M值,可以使用动态规划方法。
我们先来看递归关系。
dp[i][curr_or] = max(dp[i + 1][curr_or], dp[i + 1][curr_or | arr[i]] + 1)
现在让我们了解DP的状态。这里, dp[i][curr_or]存储子数组arr[i…N-1]的最长子序列,因此curr_or在与该子序列进行 OR 运算时给出M。在每一步,要么选择索引i并更新curr_or,要么拒绝索引i并继续。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define maxN 20
#define maxM 64
// To store the states of DP
int dp[maxN][maxM];
bool v[maxN][maxM];
// Function to return the required length
int findLen(int* arr, int i, int curr,
int n, int m)
{
// Base case
if (i == n) {
if (curr == m)
return 0;
else
return -1;
}
// If the state has been solved before
// return the value of the state
if (v[i][curr])
return dp[i][curr];
// Setting the state as solved
v[i][curr] = 1;
// Recurrence relation
int l = findLen(arr, i + 1, curr, n, m);
int r = findLen(arr, i + 1, curr | arr[i], n, m);
dp[i][curr] = l;
if (r != -1)
dp[i][curr] = max(dp[i][curr], r + 1);
return dp[i][curr];
}
// Driver code
int main()
{
int arr[] = { 3, 7, 2, 3 };
int n = sizeof(arr) / sizeof(int);
int m = 3;
int ans = findLen(arr, 0, 0, n, m);
if (ans == -1)
cout << 0;
else
cout << ans;
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int maxN = 20;
static int maxM = 64;
// To store the states of DP
static int [][]dp = new int[maxN][maxM];
static boolean [][]v = new boolean[maxN][maxM];
// Function to return the required length
static int findLen(int[] arr, int i,
int curr, int n, int m)
{
// Base case
if (i == n)
{
if (curr == m)
return 0;
else
return -1;
}
// If the state has been solved before
// return the value of the state
if (v[i][curr])
return dp[i][curr];
// Setting the state as solved
v[i][curr] = true;
// Recurrence relation
int l = findLen(arr, i + 1, curr, n, m);
int r = findLen(arr, i + 1, curr | arr[i], n, m);
dp[i][curr] = l;
if (r != -1)
dp[i][curr] = Math.max(dp[i][curr], r + 1);
return dp[i][curr];
}
// Driver code
public static void main(String []args)
{
int arr[] = { 3, 7, 2, 3 };
int n = arr.length;
int m = 3;
int ans = findLen(arr, 0, 0, n, m);
if (ans == -1)
System.out.println(0);
else
System.out.println(ans);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
import numpy as np
maxN = 20
maxM = 64
# To store the states of DP
dp = np.zeros((maxN, maxM));
v = np.zeros((maxN, maxM));
# Function to return the required length
def findLen(arr, i, curr, n, m) :
# Base case
if (i == n) :
if (curr == m) :
return 0;
else :
return -1;
# If the state has been solved before
# return the value of the state
if (v[i][curr]) :
return dp[i][curr];
# Setting the state as solved
v[i][curr] = 1;
# Recurrence relation
l = findLen(arr, i + 1, curr, n, m);
r = findLen(arr, i + 1, curr | arr[i], n, m);
dp[i][curr] = l;
if (r != -1) :
dp[i][curr] = max(dp[i][curr], r + 1);
return dp[i][curr];
# Driver code
if __name__ == "__main__" :
arr = [ 3, 7, 2, 3 ];
n = len(arr);
m = 3;
ans = findLen(arr, 0, 0, n, m);
if (ans == -1) :
print(0);
else :
print(ans);
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
static int maxN = 20;
static int maxM = 64;
// To store the states of DP
static int [,]dp = new int[maxN,maxM];
static bool [,]v = new bool[maxN,maxM];
// Function to return the required length
static int findLen(int[] arr, int i,
int curr, int n, int m)
{
// Base case
if (i == n)
{
if (curr == m)
return 0;
else
return -1;
}
// If the state has been solved before
// return the value of the state
if (v[i,curr])
return dp[i,curr];
// Setting the state as solved
v[i,curr] = true;
// Recurrence relation
int l = findLen(arr, i + 1, curr, n, m);
int r = findLen(arr, i + 1, curr | arr[i], n, m);
dp[i,curr] = l;
if (r != -1)
dp[i,curr] = Math.Max(dp[i,curr], r + 1);
return dp[i,curr];
}
// Driver code
public static void Main(String []args)
{
int []arr = { 3, 7, 2, 3 };
int n = arr.Length;
int m = 3;
int ans = findLen(arr, 0, 0, n, m);
if (ans == -1)
Console.WriteLine(0);
else
Console.WriteLine(ans);
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
3
时间复杂度: O(N * maxArr) 其中 maxArr 是数组中的最大元素。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。