首页 >后端开发 >C++ >计算长度为N的二进制字符串,它们是子字符串的重复拼接

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

WBOY
WBOY转载
2023-09-07 10:13:061411浏览

计算长度为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中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除