📌  相关文章
📜  二进制字符串的最长子序列可被3整除

📅  最后修改于: 2021-04-29 14:28:49             🧑  作者: Mango

给定长度为N的二进制字符串S ,任务是找到其中最长的子序列的长度,该长度可被3整除。子序列中的前导零是允许的。

例子:

天真的方法:生成所有可能的子序列,并检查它们是否可被3整除。为此的时间复杂度将为O((2 N )* N)

高效的方法:动态编程可以用来解决这个问题。让我们看一下DP的状态。
DP [I] [R]将存储的子S [1 … N-1],使得它提供了(3 – r)的其余部分的最长子序列%3当除以3。
现在让我们编写递归关系。

由于以下两种选择,因此得出了递归:

  1. 在子序列中包括当前索引i 。因此, r将更新为r =(r * 2 + s [i])%3
  2. 不要在子序列中包括当前索引。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define N 100
  
int dp[N][3];
bool v[N][3];
  
// Function to return the length of the
// largest sub-string divisible by 3
int findLargestString(string& s, int i, int r)
{
    // Base-case
    if (i == s.size()) {
        if (r == 0)
            return 0;
        else
            return INT_MIN;
    }
  
    // If the state has been solved
    // before then return its value
    if (v[i][r])
        return dp[i][r];
  
    // Marking the state as solved
    v[i][r] = 1;
  
    // Recurrence relation
    dp[i][r]
        = max(1 + findLargestString(s, i + 1,
                                    (r * 2 + (s[i] - '0')) % 3),
              findLargestString(s, i + 1, r));
    return dp[i][r];
}
  
// Driver code
int main()
{
    string s = "101";
  
    cout << findLargestString(s, 0, 0);
  
    return 0;
}


Java
// Java implementation of th approach 
class GFG 
{
  
    final static int N = 100 ;
    final static int INT_MIN = Integer.MIN_VALUE;
      
    static int dp[][] = new int[N][3]; 
    static int v[][] = new int[N][3]; 
      
      
    // Function to return the length of the 
    // largest sub-string divisible by 3 
    static int findLargestString(String s, int i, int r) 
    { 
        // Base-case 
        if (i == s.length())
        { 
            if (r == 0) 
                return 0; 
            else
                return INT_MIN; 
        } 
      
        // If the state has been solved 
        // before then return its value 
        if (v[i][r] == 1) 
            return dp[i][r]; 
      
        // Marking the state as solved 
        v[i][r] = 1; 
      
        // Recurrence relation 
        dp[i][r] = Math.max(1 + findLargestString(s, i + 1, 
                          (r * 2 + (s.charAt(i) - '0')) % 3), 
                            findLargestString(s, i + 1, r)); 
        return dp[i][r];
    }
      
    // Driver code 
    public static void main (String[] args) 
    { 
        String s = "101"; 
      
        System.out.print(findLargestString(s, 0, 0)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach 
import numpy as np
import sys
  
N = 100
INT_MIN = -(sys.maxsize - 1)
  
dp = np.zeros((N, 3)); 
v = np.zeros((N, 3)); 
  
# Function to return the length of the 
# largest sub-string divisible by 3 
def findLargestString(s, i, r) : 
  
    # Base-case 
    if (i == len(s)) :
        if (r == 0) :
            return 0; 
        else :
            return INT_MIN; 
  
    # If the state has been solved 
    # before then return its value 
    if (v[i][r]) :
        return dp[i][r]; 
  
    # Marking the state as solved 
    v[i][r] = 1; 
  
    # Recurrence relation 
    dp[i][r] = max(1 + findLargestString(s, i + 1, 
                  (r * 2 + (ord(s[i]) - ord('0'))) % 3),
                       findLargestString(s, i + 1, r)); 
                  
    return dp[i][r]; 
  
# Driver code 
if __name__ == "__main__" : 
  
    s = "101"; 
  
    print(findLargestString(s, 0, 0)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of th approach 
using System;
using System.Collections.Generic;
  
class GFG 
{
  
    readonly static int N = 100 ;
    readonly static int INT_MIN = int.MinValue;
      
    static int [,]dp = new int[N, 3]; 
    static int [,]v = new int[N, 3]; 
      
    // Function to return the length of the 
    // largest sub-string divisible by 3 
    static int findLargestString(String s, int i, int r) 
    { 
        // Base-case 
        if (i == s.Length)
        { 
            if (r == 0) 
                return 0; 
            else
                return INT_MIN; 
        } 
      
        // If the state has been solved 
        // before then return its value 
        if (v[i, r] == 1) 
            return dp[i, r]; 
      
        // Marking the state as solved 
        v[i, r] = 1; 
      
        // Recurrence relation 
        dp[i, r] = Math.Max(1 + findLargestString(s, i + 1, 
                                (r * 2 + (s[i] - '0')) % 3), 
                            findLargestString(s, i + 1, r)); 
        return dp[i, r];
    }
      
    // Driver code 
    public static void Main(String[] args) 
    { 
        String s = "101"; 
      
        Console.Write(findLargestString(s, 0, 0)); 
    } 
}
  
// This code is contributed by 29AjayKumar


输出:
2

时间复杂度: O(n)