搜尋
首頁後端開發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刪除
c#vs. c:每種語言都擅長c#vs. c:每種語言都擅長Apr 12, 2025 am 12:08 AM

C#適合需要高開發效率和跨平台支持的項目,而C 適用於需要高性能和底層控制的應用。 1)C#簡化開發,提供垃圾回收和豐富類庫,適合企業級應用。 2)C 允許直接內存操作,適用於遊戲開發和高性能計算。

繼續使用C:耐力的原因繼續使用C:耐力的原因Apr 11, 2025 am 12:02 AM

C 持續使用的理由包括其高性能、廣泛應用和不斷演進的特性。 1)高效性能:通過直接操作內存和硬件,C 在系統編程和高性能計算中表現出色。 2)廣泛應用:在遊戲開發、嵌入式系統等領域大放異彩。 3)不斷演進:自1983年發布以來,C 持續增加新特性,保持其競爭力。

C和XML的未來:新興趨勢和技術C和XML的未來:新興趨勢和技術Apr 10, 2025 am 09:28 AM

C 和XML的未來發展趨勢分別為:1)C 將通過C 20和C 23標準引入模塊、概念和協程等新特性,提升編程效率和安全性;2)XML將繼續在數據交換和配置文件中佔據重要地位,但會面臨JSON和YAML的挑戰,並朝著更簡潔和易解析的方向發展,如XMLSchema1.1和XPath3.1的改進。

現代C設計模式:構建可擴展和可維護的軟件現代C設計模式:構建可擴展和可維護的軟件Apr 09, 2025 am 12:06 AM

現代C 設計模式利用C 11及以後的新特性實現,幫助構建更靈活、高效的軟件。 1)使用lambda表達式和std::function簡化觀察者模式。 2)通過移動語義和完美轉發優化性能。 3)智能指針確保類型安全和資源管理。

C多線程和並發:掌握並行編程C多線程和並發:掌握並行編程Apr 08, 2025 am 12:10 AM

C 多線程和並發編程的核心概念包括線程的創建與管理、同步與互斥、條件變量、線程池、異步編程、常見錯誤與調試技巧以及性能優化與最佳實踐。 1)創建線程使用std::thread類,示例展示瞭如何創建並等待線程完成。 2)同步與互斥使用std::mutex和std::lock_guard保護共享資源,避免數據競爭。 3)條件變量通過std::condition_variable實現線程間的通信和同步。 4)線程池示例展示瞭如何使用ThreadPool類並行處理任務,提高效率。 5)異步編程使用std::as

C深度潛水:掌握記憶管理,指針和模板C深度潛水:掌握記憶管理,指針和模板Apr 07, 2025 am 12:11 AM

C 的內存管理、指針和模板是核心特性。 1.內存管理通過new和delete手動分配和釋放內存,需注意堆和棧的區別。 2.指針允許直接操作內存地址,使用需謹慎,智能指針可簡化管理。 3.模板實現泛型編程,提高代碼重用性和靈活性,需理解類型推導和特化。

C和系統編程:低級控制和硬件交互C和系統編程:低級控制和硬件交互Apr 06, 2025 am 12:06 AM

C 適合系統編程和硬件交互,因為它提供了接近硬件的控制能力和麵向對象編程的強大特性。 1)C 通過指針、內存管理和位操作等低級特性,實現高效的系統級操作。 2)硬件交互通過設備驅動程序實現,C 可以編寫這些驅動程序,處理與硬件設備的通信。

使用C的遊戲開發:構建高性能遊戲和模擬使用C的遊戲開發:構建高性能遊戲和模擬Apr 05, 2025 am 12:11 AM

C 適合構建高性能遊戲和仿真係統,因為它提供接近硬件的控制和高效性能。 1)內存管理:手動控制減少碎片,提高性能。 2)編譯時優化:內聯函數和循環展開提昇運行速度。 3)低級操作:直接訪問硬件,優化圖形和物理計算。

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

熱工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。