計算長度為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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

熱門話題

MySQL中如何使用LOCATE函數來尋找子字串在字串中的位置在MySQL中,有許多函數可以用來處理字串。其中,LOCATE函數是一種非常有用的函數,可以用來尋找子字串在字串中的位置。 LOCATE函數的語法如下:LOCATE(substring,string,[position])其中,substring為要找的子字串,string為要在其中

給定兩個字串str_1和str_2。目標是使用遞歸過程計算字串str1中子字串str2的出現次數。遞歸函數是在其定義中呼叫自身的函數。如果str1是"Iknowthatyouknowthatiknow",str2是"know"出現次數為-3讓我們透過範例來理解。例如輸入str1="TPisTPareTPamTP",str2="TP";輸出Countofoccurrencesofasubstringrecursi

該函數與strtok()函數類似。唯一的關鍵區別是_r,它被稱為可重入函數。可重入函數是執行過程中可以被中斷的函數。這種類型的函數可用於恢復執行。因此,可重入函數是線程安全的,這意味著它們可以安全地被線程中斷,而不會造成任何損害。 strtok_r()函數有一個稱為上下文的額外參數。這樣函數就可以在正確的位置恢復。 strtok_r()函數的語法如下:#include<string.h>char*strtok_r(char*string,constchar*limiter,char**

在這個問題中,我們需要找到給定字串的最長非遞增子序列。非遞增的意思是字元要麼相同,要麼按降序排列。由於二進位字串僅包含“0”和“1”,因此產生的字串應以“1”開頭並以“0”結尾,或以“0”或“1”開頭和結尾。為了解決這個問題,我們將統計字串每個位置的前綴“1”和後綴“0”,並找到前綴“1”和後綴“0”的最大和。問題陳述-我們給了二進位字串str。我們需要從給定的字串中找到最長的非遞增子序列。範例Input–str="010100"Output–4說明最長的非遞

在給定的問題中,我們得到一個由0和1組成的字串;我們需要找出以1開頭的所有排列的總數。由於答案可能是一個巨大的數字,所以我們將其取模1000000007後輸出。 Input:str="10101001001"Output:210Input:str="101110011"Output:56我們將透過應用一些組合數學和建立一些公式來解決這個問題。解的方法在這個方法中,我們將計算0和1的數量。現在假設n是我們字串中出現的1的數量,m是我們字串中出現的0

pack()函數將資料打包到二進位字串中。語法pack(format,args)參數格式-要使用的格式。以下是可能的值-a-NUL填充字串A-空格填充字串h-十六進位字串,低半位元組在前H-十六進位字串,高半位元組在前c-帶符號字元C-無符號字元s-帶符號短字元(始終為16位,機器字節順序)S-無符號短整型(始終為16位,機器字節順序)n-無符號短整型(始終為16位,大端字節順序)v-無符號短整型(始終為16位,小端字節順序)i-有符號整數(取決於機器的大小和字節順序)I-無符號整數(取決

這篇文章將為大家詳細講解有關PHP返回一個字符串在另一個字符串中開始位置到結束位置的字符串,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP中使用substr()函數從字串中擷取子字串substr()函數可從字串中擷取指定範圍內的字元。其語法如下:substr(string,start,length)其中:string:要從中提取子字串的原始字串。 start:子字串開始位置的索引(從0開始)。 length(可選):子字串的長度。如果未指定,則提

正規表示式是一種強大的文字處理工具,它可以用來匹配特定模式的字串。在PHP中,正規表示式常用於字串處理、表單驗證、搜尋和替換等方面。本文將介紹如何使用PHP的正規表示式從字串中提取特定字元到結尾的子字串。首先,讓我們來看一個例子。假設我們有一個字串$str,其中包含多個以「http://」開頭的URL,我們想要提取這些URL,並儲存在一
