给定一个整数N ,任务是找到可以使用2 * N个括号形成的不同括号序列的数量,以使该序列不是N周期的。
A bracket sequence str of length 2 * N is said to be N-periodic if the sequence can be split into two equal substrings having same regular bracket sequence.
A regular bracket sequence is a sequence in the following way:
- An empty string is a regular bracket sequence.
- If s & t are regular bracket sequences, then s + t is a regular bracket sequence.
例子:
Input: N = 3
Output: 5
Explanation:
There will be 5 distinct regular bracket sequences of length 2 * N = ()()(), ()(()), (())(), (()()), ((()))
Now, none of the sequences are N-periodic. Therefore, the output is 5.
Input: N = 4
Output: 12
Explanation:
There will be 14 distinct regular bracket sequences of length 2*N which are
()()()(), ()()(()), ()(())(), ()(()()), ()((())), (())()(), (())(()), (()())(), (()()()), (()(())), ((()))(), ((())()), ((()())), (((())))
Out of these 14 regular sequences, two of them are N periodic which are
()()()() and (())(()). They have a period of N.
Therefore, the distinct regular bracket sequences of length 2 * N which are not N-periodic are 14 – 2 = 12.
方法:想法是计算可能的长度为2 * N的规则括号序列的总数,然后从中减去N周期的括号序列的数量。步骤如下:
- 要查找长度为2 * N的常规括号序列的数量,请使用加泰罗尼亚语公式。
- 对于长度为2 * N的序列为N个周期,N应该是偶数,因为如果N为奇数,则长度2 * N的序列就不能为规则序列,并且周期为N。
- 由于两个相似的非规则括号序列的串联不能使序列规则,因此长度N的两个子序列都应是规则的。
- 从长度为2 * N的常规括号序列的数量中减少长度为N (如果N为偶数)的规则括号序列的数量,以获得所需的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that finds the value of
// Binomial Coefficient C(n, k)
unsigned long int
binomialCoeff(unsigned int n,
unsigned int k)
{
unsigned long int res = 1;
// Since C(n, k) = C(n, n - k)
if (k > n - k)
k = n - k;
// Calculate the value of
// [n*(n - 1)*---*(n - k + 1)] /
// [k*(k - 1)*---*1]
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
// Return the C(n, k)
return res;
}
// Binomial coefficient based function to
// find nth catalan number in O(n) time
unsigned long int catalan(unsigned int n)
{
// Calculate value of 2nCn
unsigned long int c
= binomialCoeff(2 * n, n);
// Return C(2n, n)/(n+1)
return c / (n + 1);
}
// Function to find possible ways to
// put balanced parenthesis in an
// expression of length n
unsigned long int findWays(unsigned n)
{
// If n is odd, not possible to
// create any valid parentheses
if (n & 1)
return 0;
// Otherwise return n/2th
// Catalan Numer
return catalan(n / 2);
}
void countNonNPeriodic(int N)
{
// Difference between counting ways
// of 2*N and N is the result
cout << findWays(2 * N)
- findWays(N);
}
// Driver Code
int main()
{
// Given value of N
int N = 4;
// Function Call
countNonNPeriodic(N);
return 0;
}
Java
// Java program for above approach
import java.io.*;
class GFG{
// Function that finds the value of
// Binomial Coefficient C(n, k)
static long binomialCoeff(int n, int k)
{
long res = 1;
// Since C(n, k) = C(n, n - k)
if (k > n - k)
k = n - k;
// Calculate the value of
// [n*(n - 1)*---*(n - k + 1)] /
// [k*(k - 1)*---*1]
for(int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
// Return the C(n, k)
return res;
}
// Binomial coefficient based function to
// find nth catalan number in O(n) time
static long catalan(int n)
{
// Calculate value of 2nCn
long c = binomialCoeff(2 * n, n);
// Return C(2n, n)/(n+1)
return c / (n + 1);
}
// Function to find possible ways to
// put balanced parenthesis in an
// expression of length n
static long findWays(int n)
{
// If n is odd, not possible to
// create any valid parentheses
if ((n & 1) == 1)
return 0;
// Otherwise return n/2th
// Catalan Numer
return catalan(n / 2);
}
static void countNonNPeriodic(int N)
{
// Difference between counting ways
// of 2*N and N is the result
System.out.println(findWays(2 * N) -
findWays(N));
}
// Driver code
public static void main (String[] args)
{
// Given value of N
int N = 4;
// Function call
countNonNPeriodic(N);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for
# the above approach
# Function that finds the value of
# Binomial Coefficient C(n, k)
def binomialCoeff(n, k):
res = 1
# Since C(n, k) = C(n, n - k)
if (k > n - k):
k = n - k
# Calculate the value of
# [n*(n - 1)*---*(n - k + 1)] /
# [k*(k - 1)*---*1]
for i in range(k):
res = res * (n - i)
res = res // (i + 1)
# Return the C(n, k)
return res
# Binomial coefficient based function to
# find nth catalan number in O(n) time
def catalan(n):
# Calculate value of 2nCn
c = binomialCoeff(2 * n, n)
# Return C(2n, n)/(n+1)
return c // (n + 1)
# Function to find possible ways to
# put balanced parenthesis in an
# expression of length n
def findWays(n):
# If n is odd, not possible to
# create any valid parentheses
if ((n & 1) == 1):
return 0
# Otherwise return n/2th
# Catalan Numer
return catalan(n // 2)
def countNonNPeriodic(N):
# Difference between counting ways
# of 2*N and N is the result
print(findWays(2 * N) - findWays(N))
# Driver code
# Given value of N
N = 4
# Function call
countNonNPeriodic(N)
# This code is contributed by divyeshrabadiya07
C#
// C# program for above approach
using System;
using System.Collections.Generic;
class GFG{
// Function that finds the value of
// Binomial Coefficient C(n, k)
static long binomialCoeff(int n, int k)
{
long res = 1;
// Since C(n, k) = C(n, n - k)
if (k > n - k)
k = n - k;
// Calculate the value of
// [n*(n - 1)*---*(n - k + 1)] /
// [k*(k - 1)*---*1]
for(int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
// Return the C(n, k)
return res;
}
// Binomial coefficient based function to
// find nth catalan number in O(n) time
static long catalan(int n)
{
// Calculate value of 2nCn
long c = binomialCoeff(2 * n, n);
// Return C(2n, n)/(n+1)
return c / (n + 1);
}
// Function to find possible ways to
// put balanced parenthesis in an
// expression of length n
static long findWays(int n)
{
// If n is odd, not possible to
// create any valid parentheses
if ((n & 1) == 1)
return 0;
// Otherwise return n/2th
// Catalan Numer
return catalan(n / 2);
}
static void countNonNPeriodic(int N)
{
// Difference between counting ways
// of 2*N and N is the result
Console.Write(findWays(2 * N) -
findWays(N));
}
// Driver Code
public static void Main(string[] args)
{
// Given value of N
int N = 4;
// Function call
countNonNPeriodic(N);
}
}
// This code is contributed by rutvik_56
Javascript
12
时间复杂度: O(N)
辅助空间: O(1)