0

0

找到给定大小的二进制字符串数组中不存在的任意排列

王林

王林

发布时间:2023-08-26 13:57:06

|

1148人浏览过

|

来源于tutorialspoint

转载

找到给定大小的二进制字符串数组中不存在的任意排列

在这个问题中,我们需要从数组中找到长度为N的所有缺失的二进制字符串。我们可以通过找到长度为N的二进制字符串的所有排列,并检查哪些排列在数组中不存在来解决这个问题。在这里,我们将看到迭代和递归的方法来解决这个问题。

问题陈述 - 我们已经给出了一个包含不同长度的二进制字符串的数组arr[],长度为N。我们需要从数组中找出所有长度为N的缺失二进制字符串。

示例例子

输入 – arr = {"111", "001", "100", "110"}, N = 3

输出 – [000, 010, 011, 101]

Explanation – 有8个长度为3的二进制字符串,因为2的3次方等于8。所以,它打印出长度为3的缺失的4个二进制字符串。

输入 – str = {‘00’, ‘10’, ‘11’}, N = 2

Output – [‘01’]

Explanation – ‘01’在数组中缺失,因此会在输出中打印出来。

方法一

在这里,我们将使用迭代的方法来找到长度为N的所有可能的二进制字符串。之后,我们将检查该字符串是否存在于数组中。如果不存在,我们将打印该字符串。

算法

  • 定义集合并使用insert()方法将数组中的所有字符串添加到集合中。

  • 用2N初始化total变量,其中2N是长度为N的字符串的总数

  • 定义变量 'cnt' 并将其初始化为零,以存储缺失组合的总数。

  • 使用循环来使'总'迭代次数,以找到所有长度为N的二进制字符串。

  • 在循环中,使用空字符串初始化“num”字符串变量。

  • 使用嵌套循环进行总共N次迭代,并从最后一次迭代开始,创建长度为N的字符串。

    秘塔AI搜索
    秘塔AI搜索

    秘塔AI搜索,没有广告,直达结果

    下载
  • 使用find()方法来检查集合是否包含当前字符串。如果是,则将‘cnt’的值增加1。

  • 如果字符串不在映射中,则打印它以显示在输出中

  • 如果“cnt”的值等于总数,则表示数组中存在所有长度为N的字符串,并打印“-1”。

Example

#include 
using namespace std;
// function to print missing combinations of a binary string of length N in an array
void printMissingCombinations(vector &arr, int N) {
   unordered_set set;
   // insert all the strings in the set
   for (string temp : arr) {
      set.insert(temp);
   }
   // get total combinations for the string of length N
   int total = (int)pow(2, N);
   // To store combinations that are present in an array
   int cnt = 0;
   // find all the combinations
   for (int p = 0; p < total; p++) {
      // Initialize empty binary string
      string bin = "";
      for (int q = N - 1; q >= 0; q--) {
          // If the qth bit is set, append '1'; append '0'.
          if (p & (1 << q)) {
              bin += '1';
          } else {
              bin += '0';
          }
      }
      // If the combination is present in an array, increment cnt
      if (set.find(bin) != set.end()) {
          cnt++;
          continue;
      } else {
          cout << bin << ", ";
      }
   }
   // If all combinations are present in an array, print -1
   if (cnt == total) {
      cout << "-1";
   }
}
int main() {
   int N = 3;
   vector arr = {"111", "001", "100", "110"};
   printMissingCombinations(arr, N);
   return 0;
}

输出

000, 010, 011, 101, 

时间复杂度 - O(N*2N),其中O(N)用于检查字符串是否存在于数组中,O(2N)用于找到所有可能的排列。

空间复杂度 - O(N),因为我们使用set来存储字符串。

方法二

在这种方法中,我们展示了使用递归方法来找到长度为N的所有可能的二进制字符串。

算法

  • 定义集合并将所有数组值插入集合中。

  • 调用generateCombinations()函数生成二进制字符串的所有组合

  • 在generateCombinations()函数中定义基本情况。如果索引等于N,则将currentCombination添加到列表中。

    • 在将‘0’或‘1’添加到当前组合后,递归调用generateCombinations()函数。

  • 获取所有组合后,检查哪些组合存在于数组中,哪些不存在。同时,打印出缺失的组合以在输出中显示。

Example

#include 
using namespace std;
// Function to generate all possible combinations of binary strings
void generateCombinations(int index, int N, string currentCombination, vector &combinations) {
   // Base case: if we have reached the desired length N, add the combination to the vector
   if (index == N) {
      combinations.push_back(currentCombination);
      return;
   }
   // Recursively generate combinations by trying both 0 and 1 at the current index
   generateCombinations(index + 1, N, currentCombination + "0", combinations);
   generateCombinations(index + 1, N, currentCombination + "1", combinations);
}
// function to print missing combinations of a binary string of length N in an array
void printMissingCombinations(vector &arr, int N) {    
   unordered_set set;
   // insert all the strings in the set
   for (string str : arr) {
      set.insert(str);
   }
   // generating all combinations of binary strings of length N
   vector combinations;
   generateCombinations(0, N, "", combinations);
   // Traverse all the combinations and check if it is present in the set or not
   for (string str : combinations) {
      // If the combination is not present in the set, print it
      if (set.find(str) == set.end()) {
          cout << str << endl;
      }
   }

   return;
}
int main(){
   int N = 3;
   vector arr = {"111", "001", "100", "110"};
   printMissingCombinations(arr, N);
   return 0;
}

输出

000
010
011
101

时间复杂度 - O(N*2N)

空间复杂度 - O(2N),因为我们将所有组合存储在数组中。

这两种方法使用相同的逻辑来解决问题。第一种方法使用迭代技术来找到长度为N的二进制字符串的所有组合,比第二种方法中使用的递归技术更快。此外,第二种方法消耗的空间比第一种方法多。

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 13.1万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号