
本文的目的是实现一个程序,用于计算由一个子字符串重复连接而成的长度为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#includeusing namespace std;// Storing all the states of recurring recursive map 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的二进制字符串,它们是子字符串的重复拼接的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444579.html
微信扫一扫
支付宝扫一扫