Home > Backend Development > C++ > body text

Count the number of different normal bracket sequences that are not N-period

PHPz
Release: 2023-08-30 23:49:13
forward
1125 people have browsed it

Count the number of different normal bracket sequences that are not N-period

In this article, we're going to delve into an intriguing problem from the realm of combinatorics and string processing: "Counting distinct regular bracket sequences which are not N periodic". This problem involves generating distinct valid bracket sequences and then filtering out sequences that are N-periodic. We'll discuss the problem, provide a C code implementation of a brute-force approach, and explain a test case.

Understanding the Problem Statement

Given an integer N, the task is to count the number of different regular bracket sequences of length 2N that are not N periods. If a sequence can be represented as a string S repeated M times, where the length of S is N and M > 1, then the sequence is N-periodic.

A regular bracket sequence is a string that can be transformed into a correct arithmetic expression by inserting characters '1' and ' ' between the original characters. For example, the sequence "(())" is regular, while ") (" and "(()" are not.

method

Due to the complexity of the problem, a direct mathematical solution may not be feasible. Instead, we need to use a programmatic approach to generate bracket sequences and count the number of bracket sequences that match our criteria.

We will use a recursive function to generate all possible bracket sequences of length 2N. While generating the sequences, we'll keep track of two important parameters: the balance of the brackets (the number of open brackets should never be less than the number of closed brackets) and the number of open brackets (should not exceed N).

In order to filter out N-period sequences, we will check each generated sequence. If the sequence is a repeat of a smaller sequence of length N, we exclude it from the count.

C Implementation

This is a brute force approach to solving a problem in C. This method generates all possible bracket sequences and checks if each sequence is N-periodic and increments the count if not. This solution is suitable for small input sizes as it has exponential time complexity and does not scale well for larger inputs.

Example

#include <bits/stdc++.h>
using namespace std;

// Function to check if string is N periodic
bool isPeriodic(string s, int N) {
   string sub = s.substr(0, N);
   for (int i = N; i < s.size(); i += N) {
      if (sub != s.substr(i, N)) return false;
   }
   return true;
}

// Function to generate all bracket sequences
void generateBrackets(string s, int open, int close, int N, int &count) {
   // If sequence is complete
   if (s.size() == 2*N) {
      if (!isPeriodic(s, N)) count++;
      return;
   }
   
   // Add an open bracket if we have some left
   if (open < N) generateBrackets(s + "(", open + 1, close, N, count);
   
   // Add a close bracket if it doesn't make the sequence invalid
   if (close < open) generateBrackets(s + ")", open, close + 1, N, count);
}

int main() {
   int N = 3;
   int count = 0;
   generateBrackets("", 0, 0, N, count);
   cout << "Count of sequences: " << count;
   return 0;
}
Copy after login

Output

Count of sequences: 5
Copy after login

Test Case

Let's consider a test case with N = 3. This code will generate all 5 distinct regular bracket sequences of length 6 that are not 3-periodic: ((())), (()()), (() )(), ()(()), ()()().

Conclusion

This article introduces a method to violently solve the problem of counting different regular bracket sequences with non-N periods. While this method can handle small-scale inputs, it is important to note that the problem has exponential complexity due to the need to generate and check all possible bracket sequences, and therefore is not suitable for large-scale inputs.

The above is the detailed content of Count the number of different normal bracket sequences that are not N-period. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template