📜  0和1的段的最大长度

📅  最后修改于: 2021-04-23 18:03:51             🧑  作者: Mango

给定一个由一和零组成的字符串。任务是找到字符串段的最大长度,以使每个段中的数字1大于0。

注意:每个细分都应该是不同的。索引从0开始。

例子:

方法:

  1. 如果start == n,则出现限制条件,返回0。
  2. 从开始到n运行一个循环,直到每个n都计算每个子数组。
  3. 如果字符为1,则将计数增加1,否则将计数0递增。
  4. 如果计数1大于0,则递归调用索引(k + 1)(即下一个索引)的函数,并添加剩余长度(即k-start + 1)。
  5. 否则,仅递归调用下一个索引k + 1的函数。
  6. 返回dp [start]。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Recursive Function to find total length of the array
// Where 1 is greater than zero
int find(int start, string adj, int n, int dp[])
{
    // If reaches till end
    if (start == n)
        return 0;
  
    // If dp is saved
    if (dp[start] != -1)
        return dp[start];
  
    dp[start] = 0;
    int one = 0, zero = 0, k;
  
    // Finding for each length
    for (k = start; k < n; k++) {
  
        // If the character scanned is 1
        if (adj[k] == '1')
            one++;
        else
            zero++;
  
        // If one is greater than zero, add total
        // length scanned till now
        if (one > zero)
            dp[start] = max(dp[start], find(k + 1, adj, n, dp)
                                           + k - start + 1);
  
        // Continue with next length
        else
            dp[start] = max(dp[start], find(k + 1, adj, n, dp));
    }
  
    // Return the value for start index
    return dp[start];
}
  
// Driver Code
int main()
{
    string adj = "100110001010001";
  
    // Size of string
    int n = adj.size();
    int dp[n + 1];
    memset(dp, -1, sizeof(dp));
    // Calling the function to find the value of function
  
    cout << find(0, adj, n, dp) << endl;
  
    return 0;
}


Java
// Java implementation of
// above approach
import java.util.*;
import java.lang.Math;
  
class GFG
{
// Recursive Function to find 
// total length of the array 
// Where 1 is greater than zero 
public static int find(int start, String adj, 
                       int n, int dp[])
{
          
    // If reaches till end 
    if (start == n) 
        return 0; 
      
    // If dp is saved 
    if (dp[start] != -1) 
        return dp[start]; 
      
    dp[start] = 0; 
    int one = 0, zero = 0, k;
      
    // Finding for each length 
    for (k = start; k < n; k++) 
    { 
      
        // If the character scanned is 1 
        if (adj.charAt(k) == '1') 
            one++; 
        else
            zero++; 
      
        // If one is greater than 
        // zero, add total length
        // scanned till now 
        if (one > zero) 
            dp[start] = Math.max(dp[start], 
                            find(k + 1, adj, n, dp) + 
                                 k - start + 1); 
      
        // Continue with next length 
        else
            dp[start] = Math.max(dp[start], 
                            find(k + 1, adj, n, dp)); 
    }
      
    return dp[start];
}
  
// Driver code
public static void main (String[] args) 
{
    String adj = "100110001010001"; 
  
    // Size of string 
    int n = adj.length(); 
    int dp[] = new int[n + 1]; 
    Arrays.fill(dp, -1);
      
        // Calling the function to find
        // the value of function
        System.out.println(find(0, adj, n, dp));
}
}
  
// This code is contributed 
// by Kirti_Mangal


Python3
# Python 3 implementation of above approach
  
# Recursive Function to find total length 
# of the array where 1 is greater than zero
def find(start, adj, n, dp):
      
    # If reaches till end
    if (start == n):
        return 0
  
    # If dp is saved
    if (dp[start] != -1):
        return dp[start]
  
    dp[start] = 0
    one = 0
    zero = 0
      
    # Finding for each length
    for k in range(start, n, 1):
          
        # If the character scanned is 1
        if (adj[k] == '1'):
            one += 1
        else:
            zero += 1
  
        # If one is greater than zero, add 
        # total length scanned till now
        if (one > zero):
            dp[start] = max(dp[start], 
                            find(k + 1, adj, n, dp) +
                                 k - start + 1)
  
        # Continue with next length
        else:
            dp[start] = max(dp[start], 
                            find(k + 1, adj, n, dp))
  
    # Return the value for start index
    return dp[start]
  
# Driver Code
if __name__ == '__main__':
    adj = "100110001010001"
  
    # Size of string
    n = len(adj)
    dp = [-1 for i in range(n + 1)]
      
    # Calling the function to find the 
    # value of function
    print(find(0, adj, n, dp))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of above approach 
using System; 
    
class GFG 
{ 
    // Recursive Function to find total length of the array 
    // Where 1 is greater than zero 
    public static int find(int start, string adj, int n, int[] dp) 
    { 
        // If reaches till end 
        if (start == n) 
            return 0; 
        
        // If dp is saved 
        if (dp[start] != -1) 
            return dp[start]; 
        
        dp[start] = 0; 
        int one = 0, zero = 0, k; 
        
        // Finding for each length 
        for (k = start; k < n; k++) { 
        
            // If the character scanned is 1 
            if (adj[k] == '1') 
                one++; 
            else
                zero++; 
        
            // If one is greater than zero, add total 
            // length scanned till now 
            if (one > zero) 
                dp[start] = Math.Max(dp[start], find(k + 1, adj, n, dp) 
                                               + k - start + 1); 
        
            // Continue with next length 
            else
                dp[start] = Math.Max(dp[start], find(k + 1, adj, n, dp)); 
        } 
        
        // Return the value for start index 
        return dp[start]; 
    } 
        
    // Driver Code
    static void Main() 
    { 
        string adj = "100110001010001"; 
    
        // Size of string 
        int n = adj.Length; 
        int[] dp = new int[n + 1]; 
        for(int i = 0; i <= n; i++)
            dp[i] = -1;
        // Calling the function to find the value of function 
        
        Console.Write(find(0, adj, n, dp) + "\n"); 
    }
    //This code is contributed by DrRoot_
}


PHP
 $zero)
            $dp[$start] = max($dp[$start], 
                         find($k + 1, $adj, $n, $dp) +
                              $k - $start + 1);
  
        // Continue with next length
        else
            $dp[$start] = max($dp[$start], 
                         find($k + 1, $adj, $n, $dp));
    }
  
    // Return the value for $start index
    return $dp[$start];
}
  
// Driver Code
$adj = "100110001010001";
  
// Size of string
$n = strlen($adj);
$dp = array_fill(0, $n + 1, -1);
  
// Calling the function 
// to find the value of function
echo find(0, $adj, $n, $dp);
  
// This code is contributed by ihritik
?>


输出:
9