A10189 | Famil Door and Brackets
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length $n$ more than any other strings!
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets;
2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string $s$ of length $m$ ( $m<=n$ ) and want to complete it to obtain a valid sequence of brackets of length $n$ . He is going to pick some strings $p$ and $q$ consisting of round brackets and merge them in a string $p+s+q$ , that is add the string $p$ at the beginning of the string $s$ and string $q$ at the end of the string $s$ .
Now he wonders, how many pairs of strings $p$ and $q$ exists, such that the string $p+s+q$ is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo $10^{9}+7$ .
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets;
2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string $s$ of length $m$ ( $m<=n$ ) and want to complete it to obtain a valid sequence of brackets of length $n$ . He is going to pick some strings $p$ and $q$ consisting of round brackets and merge them in a string $p+s+q$ , that is add the string $p$ at the beginning of the string $s$ and string $q$ at the end of the string $s$ .
Now he wonders, how many pairs of strings $p$ and $q$ exists, such that the string $p+s+q$ is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo $10^{9}+7$ .
输入格式
First line contains $n$ and $m$ ( $1<=m<=n<=100000,n-m<=2000$ ) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string $s$ of length $m$ consisting of characters '(' and ')' only.
The second line contains string $s$ of length $m$ consisting of characters '(' and ')' only.
输出格式
Print the number of pairs of string $p$ and $q$ such that $p+s+q$ is a valid sequence of round brackets modulo $10^{9}+7$ .
输入输出样例
输入 #1
4 1 (
输出 #1
4
输入 #2
4 4 (())
输出 #2
1
输入 #3
4 3 (((
输出 #3
0
In the first sample there are four different valid pairs:
1. $p=$ "(", $q=$ "))"
2. $p=$ "()", $q=$ ")"
3. $p=$ "", $q=$ "())"
4. $p=$ "", $q=$ ")()"
In the second sample the only way to obtain a desired string is choose empty $p$ and $q$ .
In the third sample there is no way to get a valid sequence of brackets.
1. $p=$ "(", $q=$ "))"
2. $p=$ "()", $q=$ ")"
3. $p=$ "", $q=$ "())"
4. $p=$ "", $q=$ ")()"
In the second sample the only way to obtain a desired string is choose empty $p$ and $q$ .
In the third sample there is no way to get a valid sequence of brackets.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted