📜  可能的最长子序列,以1开始和结束,中间以0填充

📅  最后修改于: 2021-04-29 07:00:11             🧑  作者: Mango

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

例子:

方法:
要解决该问题,我们需要执行以下步骤:

  • 首先,预先计算并存储在前缀数组中,分别出现“ 1”“ 0”
  • 初始化两个整数ij ,其中i是第一个字符串和第二个字符串之间的分隔点,而j是第二个字符串和第三个字符串之间的分隔点。
  • 迭代ij的所有可能值(0 <= i

下面是上述方法的实现:

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


输出:
12






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