给定一个整数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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live