📜  以 1 开始和结束并在中间填充 0 的最长子序列

📅  最后修改于: 2021-09-03 15:00:22             🧑  作者: Mango

给定一个二进制字符串s ,任务是找到最长子序列的长度,该子序列可以分为三个子字符串,使得第一个和第三个子字符串为空或填充为 1,中间的子字符串为空或填充为0.

例子:

方法:
要解决此问题,我们需要按照以下步骤操作:

  • 首先,预先计算并存储在前缀数组中,分别出现“1”“0”
  • 初始化两个整数ij ,其中i将是第一个和第二个字符串之间的分区点, j 将是第二个和第三个字符串之间的分区点。
  • 迭代i & j (0 <= i < j <=n) 的所有可能值,并找到满足给定条件的子序列的最大可能长度。

下面是上述方法的实现:

C++
// C++ Program to find the
// longest subsequence possible
// that starts and ends with 1
// and filled with 0 in the middle
 
#include 
using namespace std;
 
int longestSubseq(string s, int length)
{
    // Prefix array to store the
    // occurences of '1' and '0'
    int ones[length + 1], zeroes[length + 1];
 
    // Initialise prefix arrays with 0
    memset(ones, 0, sizeof(ones));
    memset(zeroes, 0, sizeof(zeroes));
 
    // Iterate over the length of the string
    for (int i = 0; i < length; i++) {
 
        // If current character is '1'
        if (s[i] == '1') {
            ones[i + 1] = ones[i] + 1;
            zeroes[i + 1] = zeroes[i];
        }
 
        // If current character is '0'
        else {
            zeroes[i + 1] = zeroes[i] + 1;
            ones[i + 1] = ones[i];
        }
    }
 
    int answer = INT_MIN;
    int x = 0;
 
    for (int i = 0; i <= length; i++) {
        for (int j = i; j <= length; j++) {
            // Add '1' available for
            // the first string
            x += ones[i];
 
            // Add '0' available for
            // the second string
            x += (zeroes[j] - zeroes[i]);
 
            // Add '1' available for
            // the third string
            x += (ones[length] - ones[j]);
 
            // Update answer
            answer = max(answer, x);
 
            x = 0;
        }
    }
 
    // Print the final result
    cout << answer << endl;
}
 
// Driver Code
int main()
{
 
    string s = "10010010111100101";
 
    int length = s.length();
 
    longestSubseq(s, length);
 
    return 0;
}


Java
// Java program to find the
// longest subsequence possible
// that starts and ends with 1
// and filled with 0 in the middle
import java.io.*;
 
class GFG{
 
static void longestSubseq(String s,
                          int length)
{
     
    // Prefix array to store the
    // occurences of '1' and '0'
    int[] ones = new int[length + 1];
    int[] zeroes = new int[length + 1];
 
    // Iterate over the length of
    // the string
    for(int i = 0; i < length; i++)
    {
         
        // If current character is '1'
        if (s.charAt(i) == '1')
        {
            ones[i + 1] = ones[i] + 1;
            zeroes[i + 1] = zeroes[i];
        }
 
        // If current character is '0'
        else
        {
            zeroes[i + 1] = zeroes[i] + 1;
            ones[i + 1] = ones[i];
        }
    }
 
    int answer = Integer.MIN_VALUE;
    int x = 0;
 
    for(int i = 0; i <= length; i++)
    {
        for(int j = i; j <= length; j++)
        {
             
            // Add '1' available for
            // the first string
            x += ones[i];
 
            // Add '0' available for
            // the second string
            x += (zeroes[j] - zeroes[i]);
 
            // Add '1' available for
            // the third string
            x += (ones[length] - ones[j]);
 
            // Update answer
            answer = Math.max(answer, x);
            x = 0;
        }
    }
 
    // Print the final result
    System.out.println(answer);
}
 
// Driver code
public static void main(String[] args)
{
    String s = "10010010111100101";
    int length = s.length();
 
    longestSubseq(s, length);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to find the
# longest subsequence possible
# that starts and ends with 1
# and filled with 0 in the middle
import sys
 
def longestSubseq(s, length):
     
    # Prefix array to store the
    # occurences of '1' and '0'
    # Initialise prefix arrays with 0
    ones = [0 for i in range(length + 1)]
    zeroes = [0 for i in range(length + 1)]
 
    # Iterate over the length of the string
    for i in range(length):
         
        # If current character is '1'
        if(s[i] == '1'):
            ones[i + 1] = ones[i] + 1
            zeroes[i + 1] = zeroes[i]
 
        # If current character is '0'
        else:
            zeroes[i + 1] = zeroes[i] + 1
            ones[i + 1] = ones[i]
 
    answer = -sys.maxsize - 1
    x = 0
 
    for i in range(length + 1):
        for j in range(i, length + 1):
             
            # Add '1' available for
            # the first string
            x += ones[i]
 
            # Add '0' available for
            # the second string
            x += (zeroes[j] - zeroes[i])
 
            # Add '1' available for
            # the third string
            x += (ones[length] - ones[j])
 
            # Update answer
            answer = max(answer, x)
            x = 0
 
    # Print the final result
    print(answer)
 
# Driver Code
S = "10010010111100101"
length = len(S)
 
longestSubseq(S, length)
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program to find the
// longest subsequence possible
// that starts and ends with 1
// and filled with 0 in the middle
using System;
 
class GFG{
 
static void longestSubseq(String s,
                          int length)
{
     
    // Prefix array to store the
    // occurences of '1' and '0'
    int[] ones = new int[length + 1];
    int[] zeroes = new int[length + 1];
 
    // Iterate over the length of
    // the string
    for(int i = 0; i < length; i++)
    {
         
        // If current character is '1'
        if (s[i] == '1')
        {
            ones[i + 1] = ones[i] + 1;
            zeroes[i + 1] = zeroes[i];
        }
 
        // If current character is '0'
        else
        {
            zeroes[i + 1] = zeroes[i] + 1;
            ones[i + 1] = ones[i];
        }
    }
 
    int answer = int.MinValue;
    int x = 0;
 
    for(int i = 0; i <= length; i++)
    {
        for(int j = i; j <= length; j++)
        {
             
            // Add '1' available for
            // the first string
            x += ones[i];
 
            // Add '0' available for
            // the second string
            x += (zeroes[j] - zeroes[i]);
 
            // Add '1' available for
            // the third string
            x += (ones[length] - ones[j]);
 
            // Update answer
            answer = Math.Max(answer, x);
            x = 0;
        }
    }
 
    // Print the readonly result
    Console.WriteLine(answer);
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "10010010111100101";
    int length = s.Length;
 
    longestSubseq(s, length);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
12

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live