搜尋
首頁後端開發C++計算長度為N的二進位字串,它們是子字串的重複拼接

計算長度為N的二進位字串,它們是子字串的重複拼接

本文的目的是實作一個程序,用於計算由一個子字串重複連接而成的長度為N的二進位字串的數量。

目標是確定透過重複連接給定文字的單一子字串,可以建立多少長度為N的二進位字串,其中N是一個正整數。

問題陳述

實作一個程序,用於計算重複連接子字串的長度為N的二進位字串的數量。

範例範例1

Let us take the Input, N = 3
Output: 2

Explanation

的中文翻譯為:

解釋

下面列出了長度為N=3的可行二進位字串,其中重複連接了一個子字串。

"000":The substring "0" is repeatedly concatenated to form this string.
"111":The substring "1" is repeatedly concatenated to form this string.

因此,當我們計算所有這些字串的總數時,我們得到的和是2。因此,輸出為2。

範例範例2

Let us take the Input, N = 8
Output: 16

Explanation

的中文翻譯為:

解釋

下面列出了長度為N=8的可行二進位字串,其中包含了子字串的重複連接。

“00000000”: The substring "0" is repeatedly concatenated to form this string.
“11111111”: The substring "1" is repeatedly concatenated to form this string.
“01010101”: The substring "01" is repeatedly concatenated to form this string.
“10101010”: The substring "10" is repeatedly concatenated to form this string.
"00110011”: The substring "0011" is repeatedly concatenated to form this string.
"11001100”: The substring "1100" is repeatedly concatenated to form this string.
"11011101”: The substring "1101" is repeatedly concatenated to form this string.
"00100010”: The substring "0010" is repeatedly concatenated to form this string.
"10111011”: The substring "1011" is repeatedly concatenated to form this string.
"01000100”: The substring "0100" is repeatedly concatenated to form this string.
"10001000”: The substring "1000" is repeatedly concatenated to form this string.
"00010001”: The substring "0001" is repeatedly concatenated to form this string.
"11101110”: The substring "1110" is repeatedly concatenated to form this string.
"01110111”: The substring "0111" is repeatedly concatenated to form this string.
"01100110”: The substring "0110" is repeatedly concatenated to form this string.
"10011001”: The substring "1001" is repeatedly concatenated to form this string.

因此,當我們將所有這些字串的總數相加時,我們得到的和為16。因此,輸出結果為16。

方法

為了計算由子字串重複連接而成的N長度二進位字串的數量,我們採用以下方法。

解決這個問題的方法和計算重複連接子字串的N長度二進位字串的數量。

可以根據以下事實解決上述問題:每個可行的字串都包含一個重複的子字串,該子字串被連接了C次。因此,所提供的字串長度N需要被C整除才能產生所有的連續字串。

因此,首先發現N的所有除數,然後對於每個可能的除數C,發現透過連接它們可以創建的所有潛在字串的總數;這個數字可以使用2C來確定。為了確定每個遞歸呼叫的總計數,對除數C應用相同的技術,然後從2C中減去它。這也將考慮到它們之間存在的重複字串的數量。

演算法

計算下面給定的子字串重複連接的長度為N的二進位字串的演算法。

  • 第一步 − 開始

  • 第二步 − 定義一個函數來計算長度為N的字串的數量,使其是其子字串的連接。

  • 第三步 - 檢查狀態是否已計算

  • 第4步 - 儲存目前遞歸呼叫的結果或計數的值

  • 步驟 5 - 迭代所有除數

  • 第6步 - 傳回所得的結果

  • 第7步 − 停止

#範例:C 程式

這是上述演算法的C程式實現,用於計算由子字串重複連接而成的N長度二進位字串的數量。

// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;

// Storing all the states of recurring recursive 
map<int, int> dp;

// Function for counting the number of strings of length n wherein thatstring is a  concatenation of its substrings
int countTheStrings(int n){

   //the single character cannot be repeated
   if (n == 1)
      return 0;
      
   // Checking whether the state is calculated already or not
   if (dp.find(n) != dp.end())
      return dp[n];
      
      // Storing those value of the result or the count for the present recursive call
   int res = 0;
   
   // Iterate through all of the divisors
   for(int d= 1; d <= sqrt(n); d++){
      if (n % d== 0){
         res += (1 << d) -  countTheStrings(d);
         int div1 = n/d;
         if (div1 != d and d!= 1)
         
            // Non-Rep = Total - Rep
            res += (1 << div1) -  countTheStrings(div1);
      }
   }
   
   // Storing the result of the above calculations
   dp[n] = res; 
   
   // Returning the obtained result
   return res;
}
int main(){
   int n = 8;
   cout<< "Count of 8-length binary strings that are repeated concatenation of a substring: "<< endl;
   cout << countTheStrings(n) << endl;
}

輸出

Count of 8-length binary strings that are repeated concatenation of a substring −
16

結論

同樣地,我們可以計算出長度為N的二進位字串,它們是子字串的重複拼接。

在本文中解決了獲取由子字串重複連接而成的N長度二進位字串的計數的挑戰。

在這裡提供了C 程式碼以及計算重複連接子字串的N長度二進位字串的演算法。

以上是計算長度為N的二進位字串,它們是子字串的重複拼接的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
C#vs. C:內存管理和垃圾收集C#vs. C:內存管理和垃圾收集Apr 15, 2025 am 12:16 AM

C#使用自動垃圾回收機制,而C 採用手動內存管理。 1.C#的垃圾回收器自動管理內存,減少內存洩漏風險,但可能導致性能下降。 2.C 提供靈活的內存控制,適合需要精細管理的應用,但需謹慎處理以避免內存洩漏。

超越炒作:評估當今C的相關性超越炒作:評估當今C的相關性Apr 14, 2025 am 12:01 AM

C 在現代編程中仍然具有重要相關性。 1)高性能和硬件直接操作能力使其在遊戲開發、嵌入式系統和高性能計算等領域佔據首選地位。 2)豐富的編程範式和現代特性如智能指針和模板編程增強了其靈活性和效率,儘管學習曲線陡峭,但其強大功能使其在今天的編程生態中依然重要。

C社區:資源,支持和發展C社區:資源,支持和發展Apr 13, 2025 am 12:01 AM

C 學習者和開發者可以從StackOverflow、Reddit的r/cpp社區、Coursera和edX的課程、GitHub上的開源項目、專業諮詢服務以及CppCon等會議中獲得資源和支持。 1.StackOverflow提供技術問題的解答;2.Reddit的r/cpp社區分享最新資訊;3.Coursera和edX提供正式的C 課程;4.GitHub上的開源項目如LLVM和Boost提陞技能;5.專業諮詢服務如JetBrains和Perforce提供技術支持;6.CppCon等會議有助於職業

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

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.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能