Description
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters “(” and “)” (without quotes)).
On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met:
It is not empty (that is n ≠ 0).
The length of the sequence is even. First charactes of the sequence are equal to “(“. Last charactes of the sequence are equal to “)”. For example, the sequence “((()))” is an RSBS but the sequences “((())” and “(()())” are not RSBS.Elena Ivanovna, Anton’s teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted.
Because the answer can be very big and Anton’s teacher doesn’t like big numbers, she asks Anton to find the answer modulo 109 + 7.
Anton thought of this task for a very long time, but he still doesn’t know how to solve it. Help Anton to solve this task and write a program that finds the answer for it!
Input
The only line of the input contains a string s — the bracket sequence given in Anton’s homework. The string consists only of characters “(” and “)” (without quotes). It’s guaranteed that the string is not empty and its length doesn’t exceed 200 000.
Output
Output one number — the answer for the task modulo 109 + 7.
Examples
input)(()()output6input()()()output7input)))output0
Note
In the first sample the following subsequences are possible:
If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence “(())”.
If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence “()”. If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence “()”. If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence “()”. If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence “()”. If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence “()”. The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.Key
题意:给一大串前后括号组成的字符串,长度为n,从中任取若干括号、按原顺序排列,要求取出的括号前一半都是“(
”,后一半都是”)
“,问有多少种组合(即组成如((((()))))
)。如)(()()
(没个括号分别编号1~6),有2 4
、2 6
、3 4
、3 6
、5 6
、2 3 4 6
6种。
)(()()
,有三个后括号,只需存储0 2 3
即可(对该数组命名为sum,并假设一共m个后括号)。然后遍历这个sum数组:对于第i个后括号,左侧有sum[i]
个前括号,右侧有m-i
个后括号(方便起见令ai=sum[i]
,bi=m-i
),以当前后括号为第一个符合符合“((((()))))
”的后括号,则当前后括号左侧的后括号都不可能满足,当前后括号右侧的前括号也不满足。则“以当前后括号为第一个符合的后括号”一共有 Code
#include#include #include #include #define MX 200005using namespace std;typedef long long LL;const int p = 1e9 + 7;int sum[MX] = { 0 }; // store the sum of all the '(' before the last ')'LL ans = 0;LL fac[200005], fac_exp[200005];LL ModExp(LL a, LL b, LL p){ LL ans = 1; while (b) { if (b & 1) ans = ans*a%p; a = a*a%p; b >>= 1; } return ans;}LL C(int n, int m){ return fac[n] % p * fac_exp[n - m] % p * fac_exp[m] % p;}int main(){ fac[0] = fac_exp[0] = 1; for (int i = 1;i <= 200000;i++) { fac[i] = (fac[i - 1] * i) % p; fac_exp[i] = ModExp(fac[i], p - 2, p); } //freopen("in.txt", "r", stdin); string s; getline(cin, s); int num_left = 0, num_right = 0; for (char c : s) { if (c == '(') ++num_left; else { sum[num_right + 1] = sum[num_right] + num_left; ++num_right; num_left = 0; } } for (int i = 1;i <= num_right;++i) { int right = num_right - i + 1; int left = sum[i]; ans += C(right - 1 + left, right); } cout << (int)(ans%p); return 0;}