Statistics

Problem Statement for "BracketSequenceDiv2"

Problem Statement

Correct bracket sequences can be defined recursively as follows:

  • The empty string "" is a correct sequence.
  • If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
  • If "X" is a correct sequence, then "(X)" is a correct sequence.
  • Each correct bracket sequence can be derived using the above rules.

Examples of correct bracket sequences include "", "()", "()()()", "(()())", and "(((())))".

A string T is called a subsequence of a string S if we can erase some (possibly none or all) characters of S to obtain T. For example, "bdf" is a subsequence of "abcdefg".

You are given a String s that consists of the characters '(' and ')' only. Let X be the number of different non-empty subsequences of s that are correct bracket sequences. Note that each distinct subsequence should only be counted once, even if it can be obtained from s in multiple ways. (See the examples for clarification.) Compute and return the value (X modulo 1,000,000,007).

Definition

Class:
BracketSequenceDiv2
Method:
count
Parameters:
String
Returns:
int
Method signature:
int count(String s)
(be sure your method is public)

Constraints

  • s will contain between 1 and 100 characters, inclusive.
  • Each character in s will be '(' or ')'.

Examples

  1. "(())("

    Returns: 2

    Correct non-empty bracket subsequences are "()" and "(())".

  2. "())"

    Returns: 1

    Only "()" is suitable.

  3. ")((("

    Returns: 0

    There are no non-empty correct bracket subsequences.

  4. "()()()()()()()()(())))(()()()()))())"

    Returns: 364675

  5. "()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()"

    Returns: 122826009

    Do not forget to take answer modulo 1,000,000,007.

  6. "(((((((((((((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))))))))))"

    Returns: 50

  7. "()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()"

    Returns: 122826009

  8. "))))))))))))(((((((((((("

    Returns: 0

  9. "())(())()(()())()()(()()())()(()()())(())(()())()(()()()()()())()()()()(())()()(())(())()()()()(()()"

    Returns: 331970039

  10. "())()(())()()(())()()(()()())(())(())(())()(())(()())()()(()()())(()())()()()()(()()())(()())(())(()"

    Returns: 604185089

  11. "()))()))(()(()((())()))()(())(()()))))(()()()"

    Returns: 1148466

  12. ")()(()))(((((()(()()(()))())))((())(())))))())(()()))()"

    Returns: 36915289

  13. "()())()())())())))()))(((())()()())(())())(()"

    Returns: 1225415

  14. "()))(()((()(()))()(())(()))(()))()(((((((()(("

    Returns: 93751

  15. ")((())()())(()()()())()()()()((())())))()((()"

    Returns: 3206464

  16. "(()()()(((()(((())()(((()((())((())((())))(((((()((())))()()))()()()())(()((())))()()((()(((()(()())"

    Returns: 452026838

  17. "())()(())))((((()))(()()((()((())())((())(()))(())()()()(((((()()))((()()(((((())))())(((((())))))(("

    Returns: 21720547

  18. "((()()())))))((()(()))(((()(()()))))))((((()(((()((()()()))(())))())()))((()))())(((()())()()))()((("

    Returns: 618775716

  19. "(())))((()()))()())(()(()(((())))()(())))))()((()))(()))()))(()(()()()(()))))))))())())(((()))()(()("

    Returns: 185911238

  20. ")())(()(())(())(()(()()((()()(((()((())((((()(()()())())))(((())((((()(()(())(()())))(((((()))(())()"

    Returns: 368876257

  21. "((())))))(())))("

    Returns: 16

  22. ")(()()("

    Returns: 3


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: