#abc312d. D - Count Bracket Sequences

D - Count Bracket Sequences

Score : 400400 points

问题描述

给定一个非空字符串 SS,其中包含字符 ()?

对于 SS 中出现的每个 ?,有 2x2^x 种方式通过用 () 替换它来得到一个新的字符串,其中 xxSS? 出现的次数。在这些方式中,找出满足括号字符串条件的方案数,结果对 998244353998244353 取模。

若字符串满足以下任一条件,则称其为括号字符串:

  • 它是一个空字符串。
  • 它是形如 (AA) 的串联形式,其中 AA 是某个括号字符串。
  • 它是形如 AABB 的串联形式,其中 AABB 是两个非空的括号字符串。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a non-empty string SS consisting of (, ), and ?.
There are 2x2^x ways to obtain a new string by replacing each ? in SS with ( and ), where xx is the number of occurrences of ? in SS. Among them, find the number, modulo 998244353998244353, of ways that yield a parenthesis string.

A string is said to be a parenthesis string if one of the following conditions is satisfied.

  • It is an empty string.
  • It is a concatenation of (, AA, and ), for some parenthesis string AA.
  • It is a concatenation of AA and BB, for some non-empty parenthesis strings AA and BB.

Constraints

  • SS is a non-empty string of length at most 30003000 consisting of (, ), and ?.

Input

The input is given from Standard Input in the following format:

SS

Output

Print the answer.

Sample Input 1

(???(?

Sample Output 1

2

Replacing SS with ()()() or (())() yields a parenthesis string.
The other replacements do not yield a parenthesis string, so 22 should be printed.

Sample Input 2

)))))

Sample Output 2

0

Sample Input 3

??????????????(????????(??????)?????????(?(??)

Sample Output 3

603032273

Print the count modulo 998244353998244353.

update @ 2024/3/10 08:53:33