计算长度为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,并存储在一
