搜索
首页后端开发C++从一个字符串数组中找出由A个0和B个1组成的最长子集的长度

从一个字符串数组中找出由A个0和B个1组成的最长子集的长度

在这个问题中,我们需要找到最多包含A个0和B1的最长子集。我们需要做的就是使用数组元素找到所有可能的子集,并找到包含最多 A 0 和 B1 的最长子集。

在本教程中,首先,我们将学习递归方法来解决问题。之后,我们将使用动态规划的方法来优化代码。

问题陈述 - 我们给出了一个包含 N 个二进制字符串的数组。此外,我们还给出了 A 和 B 整数。我们需要使用给定的二进制字符串创建最长的子集,使其不包含超过 A 0 和 B1。

示例

Input –  arr = {"101", "0", "101", "0", "1"}, A = 2, B = 1
Output – 3

说明

最长的子集是{ "0", "0", "1"},包含2个0和1个1。

Input –  arr = {"0", "101", "0", "1"}, A = 3, B = 3
Output – 3

说明

最长的子集是{"0", "101", "0", "1"},3个0和3个1。

方法 1

在本节中,我们将学习一种使用递归的简单方法。我们将使用数组元素构造所有可能的子集,并找到包含 A 0 和 B 1 的最长子集。

算法

  • 步骤 1 - 定义 countZeros() 函数来计算给定二进制字符串中零的总数。

  • 步骤 1.1 - 将“count”变量初始化为零。

  • 步骤 1.2 - 使用 for 循环遍历字符串。

  • 步骤 1.3 - 如果第 i 个索引处的字符为“0”,则将“cnt”的值增加 1。

  • 步骤 1.2 - 返回“cnt”变量的值。

  • 步骤 2 - getMaxSubsetLen() 返回整数值并采用向量 arr、int A、int B 和索引作为参数。

  • 步骤 3 - 定义函数内的基本情况。如果索引等于向量的大小,或者 A 和 B 的值均为零,则返回 0。

  • 第 4 步 - 现在,计算索引处字符串中零的总数。

  • 第 5 步 - 从字符串长度中减去 1 的总数,即可得出 1 的总数。

  • 第 6 步 - 将“第一个”变量初始化为 0。

  • 步骤 7 - 如果 0 和 1 的总数分别小于 A 和 B,则包含当前的二进制字符串。存储 1 + 函数递归调用的返回值。在进行递归调用时,从 A 和 B 中减去 0 和 1。

  • 第 8 步 - 排除当前字符串并将结果值存储在“第二个”变量中。

  • 第 9 步 - 返回第一个和第二个的最大值。

示例

#include <bits/stdc++.h>
using namespace std;
// function to count the number of 0's in a string
int countZeros(string s){

   // initialize count variable to 0
   int count = 0;
   
   // traverse the string
   for (int i = 0; i < s.size(); i++){
   
      // if the current character is 0, the increment count
      if (s[i] == '0'){
         count++;
      }
   }
   
   // return count
   return count;
}

// recursive function to find the maximum length of a subset of strings according to the given condition.
int getMaxSubsetLen(vector<string> arr, int A, int B, int index){

   // base case
   // if all the strings are traversed, or A + B becomes 0
   if (index == arr.size() || A + B == 0){
      return 0;
   }
   
   // total number of 0's in arr[index] string
   int zero = countZeros(arr[index]);
   
   // total number of 1's in arr[index] string
   int one = arr[index].size() - zero;
   
   // Stores the length of the subset, if arr[i] is included.
   int first = 0;
   
   // if the number of 0's and 1's in arr[index] is less than or equal to A and B, respectively, then include the string
   if (zero <= A && one <= B){
      first = 1 + getMaxSubsetLen(arr, A - zero, B - one, index + 1);
   }
   
   // Stores the length of the subset, if arr[i] is not included.
   int second = getMaxSubsetLen(arr, A, B, index + 1);
   
   // return the maximum of the first and second
   return max(first, second);
}

// Driver Code
int main(){
   vector<string> arr = {"101", "0", "101", "0", "1"};
   int A = 2, B = 1;
   cout << "The maximum length of the subset consisting at most A 0s and B 1s is - " <<getMaxSubsetLen(arr, A, B, 0);
   return 0;
}

输出

The maximum length of the subset consisting at most A 0s and B 1s is - 3

时间复杂度 - O(2N),因为我们使用 N 个数组元素找到所有可能的子集。

空间复杂度 - O(1)

方法2

我们在本节中对上述方法进行了优化。我们使用动态规划的方法来解决这个问题。它存储前一个状态的结果,以降低问题的时间复杂度。

算法

  • 第 1 步 - 定义 countZeros() 函数来计算特定二进制字符串中零的总数,就像我们在上述方法中所做的那样。

  • 步骤 2 - 创建一个大小为 A x B x N 的 3 维向量来存储先前状态的结果。在列表中,当总 0 等于 A 且 1 等于 B 时,我们将在索引“I”处存储子集的长度。此外,将其作为 getMaxSubsetLen() 函数的参数传递。

  • 第 3 步 - 按照我们在上述方法中所做的那样定义基本情况。

  • 步骤 4 - 如果 dpTable[A][B][index] 的值大于 0,则表示状态已计算并返回其值。

  • 第 5 步 - 计算当前字符串中 0 和 1 的总数。

  • 第 6 步 - 获取包含和排除当前字符串后的结果值。

  • 第 7 步 - 使用 max() 函数获取第一个和第二个的最大值,并将其存储在 dpTable[A][B][index] 中后返回

示例

#include <bits/stdc++.h>
using namespace std;
// function to count the number of 0's in a string
int countZeros(string s){

   // initialize count variable to 0
   int count = 0;
   
   // traverse the string
   for (int i = 0; i < s.size(); i++){
   
      // if the current character is 0, the increment count
      if (s[i] == '0'){
         count++;
      }
   }
   
   // return count
   return count;
}

// recursive function to find the maximum length of a subset of strings according to the given condition.
int getMaxSubsetLen(vector<string> array, int A, int B, int index, vector<vector<vector<int>>> &dpTable){

   // base case
   if (index == array.size() || A + B == 0){
      return 0;
   }
   
   // return if already calculated
   if (dpTable[A][B][index] > 0){
      return dpTable[A][B][index];
   }
   
   // total number of 0's in the current string
   int zero = countZeros(array[index]);
   
   // total number of 1's in the current string
   int one = array[index].size() - zero;
   
   // to store the length of the subset can be formed by including the current string
   int first = 0;
   
   // if the total number of 0's and 1's in the current string is less than or equal to A and B, respectively
   if (zero <= A && one <= B){
      first = 1 + getMaxSubsetLen(array, A - zero, B - one, index + 1, dpTable);
   }
   
   // to store the length of the subset can be formed by excluding the current string
   int second = getMaxSubsetLen(array, A, B, index + 1, dpTable);
   
   // store the maximum of the first and second, and return
   return dpTable[A][B][index] = max(first, second);
}
int main(){
   vector<string> arr = {"101", "0", "101", "0", "1"};
   int A = 2, B = 1;
   vector<vector<vector<int>>> dpTable(A + 1, vector<vector<int>>(B + 1, vector<int>(arr.size() + 1, 0)));
   cout << "The maximum length of the subset consisting at most A 0s and B 1s is - " << getMaxSubsetLen(arr, A, B, 0, dpTable);
   return 0;
}

输出

The maximum length of the subset consisting at most A 0s and B 1s is - 3

时间复杂度 - O(A*B*N),因为我们需要填充 3D 列表才能获得结果。

空间复杂度 - O(A*B*N),因为我们使用 3D 列表进行动态规划方法。

以上是从一个字符串数组中找出由A个0和B个1组成的最长子集的长度的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
java怎么对字符串排序java怎么对字符串排序Apr 02, 2024 am 02:18 AM

Java 中对字符串排序的方法:使用 Arrays.sort() 方法对字符串数组按升序排序。使用 Collections.sort() 方法对字符串列表按升序排序。使用 Comparator 接口对字符串进行自定义排序。

\0在c语言中是什么意思\0在c语言中是什么意思Apr 27, 2024 pm 10:54 PM

C 语言中,\0 是字符串的结束标志,称为空字符或终止符。由于字符串在内存中以字节数组形式存储,编译器通过 \0 识别字符串结束,确保正确处理字符串。\0 工作原理:编译器遇到 \0 时停止读取字符,之后的字符被忽略。\0 自身不占存储空间。好处包括可靠的字符串处理、提高效率(无需扫描整个数组查找结束)以及方便比较和操作。

args在java中是什么意思args在java中是什么意思Apr 25, 2024 pm 10:15 PM

args 在 Java 中表示命令行参数,是一个字符串数组,包含程序启动时传递给它的参数列表。它仅在 main 方法中可用,其默认值为一个空数组,通过索引可以访问每个参数。args 用于接收和处理命令行参数,从而在程序启动时进行配置或提供输入数据。

在C语言环境下如何对中文字符进行排序?在C语言环境下如何对中文字符进行排序?Feb 18, 2024 pm 02:10 PM

如何在C语言编程软件中实现中文字符排序功能?在现代社会,中文字符排序功能在很多软件中都是必不可少的功能之一。无论是在文字处理软件、搜索引擎还是数据库系统中,都需要对中文字符进行排序,以便更好地展示和处理中文文本数据。而在C语言编程中,如何实现中文字符排序功能呢?下面将简要介绍一种方法。首先,为了在C语言中实现中文字符排序功能,我们需要使用到字符串比较函数。然

C++ 函数对程序性能有哪些影响?C++ 函数对程序性能有哪些影响?Apr 12, 2024 am 09:39 AM

函数对C++程序性能的影响包括函数调用开销、局部变量和对象分配开销:函数调用开销:包括堆栈帧分配、参数传递和控制权转移,对小函数影响显著。局部变量和对象分配开销:大量局部变量或对象创建和销毁会导致堆栈溢出和性能下降。

C语言程序的启动点是哪里?C语言程序的启动点是哪里?Feb 20, 2024 pm 12:12 PM

C语言程序的运行起点是什么?C语言作为一种高级编程语言,是一种十分常用的编程语言之一。在学习C语言的过程中,很多人都会对C程序的运行起点感到困惑。那么,C语言程序的运行起点到底是什么呢?答案是main函数。在C语言程序中,程序的执行都是从main函数的开始处开始的。main函数是C语言程序的入口点,也是程序员定义的第一个被执行的函数。它的主要作用是用来定义程

探索 PHP 数组去重算法的复杂度探索 PHP 数组去重算法的复杂度Apr 28, 2024 pm 05:54 PM

PHP数组去重算法的复杂度:array_unique():O(n)array_flip()+array_keys():O(n)foreach循环:O(n^2)

如何在 Golang 中用匿名数组反序列化 json?如何在 Golang 中用匿名数组反序列化 json?Feb 14, 2024 am 11:24 AM

我从外部服务器接收此json:[["010117"、"070117"、"080117"]、["080117"、"140117"、"150117"]、["150117"、"210117"、"220117"]]我需要解析它packagemainimport("encoding/json""fmt""io""os""runtime")typeRangestruct{FromstringTostring

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),