A13163 | Beautiful Bracket Sequence (hard version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
This is the hard version of this problem. The only difference is the limit of $n$ - the length of the input string. In this version, $1 \leq n \leq 10^6$ .
Let's define a correct bracket sequence and its depth as follow:
- An empty string is a correct bracket sequence with depth $0$ .
- If "s" is a correct bracket sequence with depth $d$ then "(s)" is a correct bracket sequence with depth $d + 1$ .
- If "s" and "t" are both correct bracket sequences then their concatenation "st" is a correct bracket sequence with depth equal to the maximum depth of $s$ and $t$ .
For a (not necessarily correct) bracket sequence $s$ , we define its depth as the maximum depth of any correct bracket sequence induced by removing some characters from $s$ (possibly zero). For example: the bracket sequence $s = $ "())(())" has depth $2$ , because by removing the third character we obtain a correct bracket sequence "()(())" with depth $2$ .
Given a string $a$ consists of only characters '(', ')' and '?'. Consider all (not necessarily correct) bracket sequences obtained by replacing all characters '?' in $a$ by either '(' or ')'. Calculate the sum of all the depths of all these bracket sequences. As this number can be large, find it modulo $998244353$ .
Hacks in this problem can be done only if easy and hard versions of this problem was solved.
Let's define a correct bracket sequence and its depth as follow:
- An empty string is a correct bracket sequence with depth $0$ .
- If "s" is a correct bracket sequence with depth $d$ then "(s)" is a correct bracket sequence with depth $d + 1$ .
- If "s" and "t" are both correct bracket sequences then their concatenation "st" is a correct bracket sequence with depth equal to the maximum depth of $s$ and $t$ .
For a (not necessarily correct) bracket sequence $s$ , we define its depth as the maximum depth of any correct bracket sequence induced by removing some characters from $s$ (possibly zero). For example: the bracket sequence $s = $ "())(())" has depth $2$ , because by removing the third character we obtain a correct bracket sequence "()(())" with depth $2$ .
Given a string $a$ consists of only characters '(', ')' and '?'. Consider all (not necessarily correct) bracket sequences obtained by replacing all characters '?' in $a$ by either '(' or ')'. Calculate the sum of all the depths of all these bracket sequences. As this number can be large, find it modulo $998244353$ .
Hacks in this problem can be done only if easy and hard versions of this problem was solved.
输入格式
The only line contains a non-empty string consist of only '(', ')' and '?'. The length of the string is at most $10^6$ .
输出格式
Print the answer modulo $998244353$ in a single line.
输入输出样例
输入 #1
??
输出 #1
1
输入 #2
(?(?))
输出 #2
9
In the first test case, we can obtain $4$ bracket sequences by replacing all characters '?' with either '(' or ')':
- "((". Its depth is $0$ ;
- "))". Its depth is $0$ ;
- ")(". Its depth is $0$ ;
- "()". Its depth is $1$ .
So, the answer is $1 = 0 + 0 + 0 + 1$ .
In the second test case, we can obtain $4$ bracket sequences by replacing all characters '?' with either '(' or ')':
- "(((())". Its depth is $2$ ;
- "()()))". Its depth is $2$ ;
- "((()))". Its depth is $3$ ;
- "()(())". Its depth is $2$ .
So, the answer is $9 = 2 + 2 + 3 + 2$ .
- "((". Its depth is $0$ ;
- "))". Its depth is $0$ ;
- ")(". Its depth is $0$ ;
- "()". Its depth is $1$ .
So, the answer is $1 = 0 + 0 + 0 + 1$ .
In the second test case, we can obtain $4$ bracket sequences by replacing all characters '?' with either '(' or ')':
- "(((())". Its depth is $2$ ;
- "()()))". Its depth is $2$ ;
- "((()))". Its depth is $3$ ;
- "()(())". Its depth is $2$ .
So, the answer is $9 = 2 + 2 + 3 + 2$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted