您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

计算长度为N的二进制字符串,它们是子字符串的重复拼接

2024/6/27 14:52:25发布24次查看
本文的目的是实现一个程序,用于计算由一个子字符串重复连接而成的长度为n的二进制字符串的数量。
目标是确定通过重复连接给定文本的单个子字符串,可以创建多少长度为n的二进制字符串,其中n是一个正整数。
问题陈述实现一个程序,用于计算重复连接子字符串的长度为n的二进制字符串的数量。
示例示例1let 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。
示例示例2let 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 substringsint 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的二进制字符串,它们是子字符串的重复拼接的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product