目录
示例
说明
方法2
算法
输出
首页 后端开发 C++ 从一个字符串数组中找出由A个0和B个1组成的最长子集的长度

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

Sep 11, 2023 pm 12:09 PM
字符串数组

从一个字符串数组中找出由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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热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 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

oracle中split()函数用法 oracle中split()函数用法 May 07, 2024 pm 01:06 PM

SPLIT() 函数通过指定的分隔符拆分字符串为数组,返回一个字符串数组,其中每个元素都是原始字符串中以分隔符分隔的部分。用法包括:将逗号分隔的值列表拆分为数组、从路径中提取文件名、将电子邮件地址拆分为用户名和域。

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 用于接收和处理命令行参数,从而在程序启动时进行配置或提供输入数据。

java中的args是什么意思 java中的args是什么意思 May 07, 2024 am 02:24 AM

args 是 Java 中 main 方法的特殊参数数组,用于获取命令行参数或外部输入的字符串数组。通过访问 args 数组,程序可以读取这些参数,并根据需要进行处理。

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

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

PHP 函数中人工智能技术的应用 PHP 函数中人工智能技术的应用 May 01, 2024 pm 01:15 PM

AI技术已与PHP函数相结合,增强了应用程序的功能。具体的AI应用包括:使用机器学习算法对文本进行分类,如朴素贝叶斯。使用自然语言处理技术进行深入文本分析,如分词和词干提取。

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

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

See all articles