C++ 重复连接子字符串的N位二进制字符串的计数

C++ 重复连接子字符串的N位二进制字符串的计数

本文的目标是实现一个程序来计算重复连接子字符串的N位二进制字符串的数量。

目标是确定通过反复连接给定文本的单个子字符串来创建多少长度为N的二进制字符串,给定一个称为N的正整数。

问题陈述

实现一个程序来计算重复连接子字符串的N位二进制字符串的数量。

示例1

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

说明

下面列出了由一个子字符串重复拼接而成,长度为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

解释

下面列出了可行的长度为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长度的二进制字符串的重复连接子串的算法。

  • 步骤1 - 开始

  • 步骤2 - 定义函数来计算长度为N的字符串数量,该字符串是其子串的连接。

  • 步骤3 - 检查状态是否已经计算过

  • 步骤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长度二进制字符串的数量,这些字符串是重复拼接子字符串的挑战。

在这里提供了计算重复拼接子字符串的N长度二进制字符串的C++编程代码和算法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程