Number of RBS
Practice
3 (2 votes)
Algorithms
Dynamic programming
2d dynamic programming
Problem
80% Success 775 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Consider a string $$S$$ of $$n$$ characters '(', ')', and '?'. Your task is to replace each character '?' with either ')' or '('. Determine the number of Regular Bracket Sequences (RBS) that is possible after these replacements.

A bracket sequence is called regular if it is possible to obtain correct arithmetic expressions by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, whereas «)(», «(()» and «(()))(» are not.

Input format

  • The first line contains one integer $$T$$ denoting the number of test cases.
  • The first line of each test case contains an integer $$n$$ that denotes the length of string $$S$$.
  • The second line of each test case contains a string that denotes the string $$S$$.

Output format

For each test case, print the number of RBS modulo \(1e9+7\) in a new line.

Constraints

\(1 \le T \le 100\\ 1 \le n \le 1000\)

The sum of $$n$$ over $$T$$ test cases does not exceed 1000.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
13 votes
Tags:
AlgorithmsDynamic ProgrammingJavaMediumOptimizationPythonTwo dimensional
Points:30
13 votes
Tags:
2-D ArrayAlgorithmsApprovedDynamic ProgrammingDynamic programmingMedium
Points:30
54 votes
Tags:
2D dynamic programmingAlgorithmsDynamic ProgrammingMediumPermutation and combination