📌  相关文章
📜  重复连接子字符串的 N 长度二进制字符串的计数

📅  最后修改于: 2021-09-17 16:08:52             🧑  作者: Mango

给定一个正整数N ,任务是找到长度为N的二进制字符串的数量,这些字符串只重复连接该字符串的一个子字符串。

例子:

方法:给定的问题可以基于观察到每个可能的字符串都有一个重复的子字符串来解决,该子字符串连接了K次,那么给定长度的字符串N必须能被K整除以生成所有结果字符串。

因此,找到N 的所有可能除数,对于每个除数,假设K找到它可以形成的所有可能字符串的计数,其连接是结果字符串,并且该计数可以通过2 K计算。现在,它们之间重复的字符串数量也必须减去,因此对除数K执行相同的操作并将其从2 K 中减去,以获得每次递归调用的总数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Store the recurring recursive states
map dp;
 
// Function to find the number of
// strings of length N such that it
// is a concatenation it substrings
int countStrings(int N)
{
     
    // Single character cant be repeated
    if (N == 1)
        return 0;
     
    // Check if this state has been
    // already calculated
    if (dp.find(N) != dp.end())
        return dp[N];
     
    // Stores the resultant count for
    // the current recursive calls
    int ret = 0;
     
    // Iterate over all divisors
    for(int div = 1; div <= sqrt(N); div++)
    {
        if (N % div == 0)
        {
             
            // Non-Repeated = Total - Repeated
            ret += (1 << div) -  countStrings(div);
             
            int div2 = N/div;
             
            if (div2 != div and div != 1)
                 
                // Non-Repeated = Total - Repeated
                ret += (1 << div2) -  countStrings(div2);
        }
    }
     
    // Store the result for the
    // further calculation
    dp[N] = ret;       
                 
    // Return resultant count
    return ret;
}
 
// Driver code
int main()
{
    int N = 6;
     
    // Function Call
    cout << countStrings(N) << endl;
}
 
// This code is contributed by ipg2016107


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Store the recurring recursive states
static HashMap dp = new HashMap();
 
// Function to find the number of
// strings of length N such that it
// is a concatenation it substrings
static int countStrings(int N)
{
     
    // Single character cant be repeated
    if (N == 1)
        return 0;
     
    // Check if this state has been
    // already calculated
    if (dp.containsKey(N))
        return dp.get(N);
     
    // Stores the resultant count for
    // the current recursive calls
    int ret = 0;
     
    // Iterate over all divisors
    for(int div = 1; div <= Math.sqrt(N); div++)
    {
        if (N % div == 0)
        {
             
            // Non-Repeated = Total - Repeated
            ret += (1 << div) -  countStrings(div);
             
            int div2 = N / div;
             
            if (div2 != div && div != 1)
                 
                // Non-Repeated = Total - Repeated
                ret += (1 << div2) -  countStrings(div2);
        }
    }
     
    // Store the result for the
    // further calculation
    dp.put(N, ret);       
                 
    // Return resultant count
    return ret;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 6;
     
    // Function Call
    System.out.print(countStrings(N));
}
}
 
// This code is contributed by code_hunt


Python3
# Python program for the above approach
 
# Store the recurring recursive states
dp = {}
 
# Function to find the number of
# strings of length N such that it
# is a concatenation it substrings
def countStrings(N):
    
    # Single character cant be repeated
    if N == 1:
        return 0
     
    # Check if this state has been
    # already calculated
    if dp.get(N, -1) != -1:
        return dp[N]
     
    # Stores the resultant count for
    # the current recursive calls
    ret = 0
     
    # Iterate over all divisors
    for div in range(1, int(N**.5)+1): 
        if N % div == 0:
             
            # Non-Repeated = Total - Repeated
            ret += (1 << div) -  countStrings(div)
             
            div2 = N//div
             
            if div2 != div and div != 1:
                 
                # Non-Repeated = Total - Repeated
                ret += (1 << div2) -  countStrings(div2)
     
    # Store the result for the
    # further calculation
    dp[N] = ret         
                 
    # Return resultant count
    return ret
 
# Driver Code
N = 6
 
# Function Call
print(countStrings(N))


Javascript


输出:
10

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程