#P7914. [CSP-S 2021] 括号序列
[CSP-S 2021] 括号序列
Problem Description
Little w encountered such a problem in a contest: given a valid bracket sequence of length , where some positions are already fixed and some are not, find how many such bracket sequences there are.
The battle-hardened little w solved it instantly. Not only that, he also felt that using such a simple template problem in an official contest was too easy, so he strengthened the problem and casually threw it to little c.
Specifically, little w defines a “super bracket sequence” as a string consisting of characters (, ), and *. For a given constant , the definition of a “valid super bracket sequence” is as follows:
()and(S)are both valid super bracket sequences, whereSis any non-empty string consisting only of no more than characters*(theSin the next two rules has the same meaning).- If both strings
AandBare valid super bracket sequences, thenABandASBare also valid super bracket sequences, whereABmeans concatenating stringAand stringB. - If string
Ais a valid super bracket sequence, then(A),(SA), and(AS)are also valid super bracket sequences. - All valid super bracket sequences can be obtained using the above 3 rules.
For example, if , then the string ((**()*(*))*)(***) is a valid super bracket sequence, but *(), (*()*), ((**))*), and (****(*)) are not. In particular, the empty string is also not considered a valid super bracket sequence.
Now you are given a super bracket sequence of length , where some positions have already been fixed, and the other positions are not fixed (denoted by ?). Little w wants to compute: in how many ways can we determine all the unknown characters one by one so that the resulting string is a valid super bracket sequence?
Poor little c cannot solve this problem, so he has to ask you for help.
Input Format
The first line contains two positive integers .
The second line contains a string of length , consisting only of (, ), *, and ?.
Output Format
Output a non-negative integer, representing the answer modulo .
7 3
(*??*??
5
10 2
???(*??(?)
19
见附件中的 bracket/bracket3.in
见附件中的 bracket/bracket3.ans
见附件中的 bracket/bracket4.in
见附件中的 bracket/bracket4.ans
Hint
[Sample Explanation #1]
The following solutions are valid:
(**)*()
(**(*))
(*(**))
(*)**()
(*)(**)
[Constraints]
| Test Point ID | Special Property | |
|---|---|---|
| None | ||
The string contains only the character ? |
||
| None |
For of the testdata, .
Translated by ChatGPT 5