CodeForces 629C Famil Door and Brackets
来源:互联网 发布:淘宝ipad客户端 编辑:程序博客网 时间:2024/04/29 09:03
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 lengthn more than any other strings!
The sequence of round brackets is called valid if and only if:
- the total number of opening brackets is equal to the total number of closing brackets;
- 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 lengthn. He is going to pick some strings p and q consisting of round brackets and merge them in a stringp + s + q, that is add the stringp at the beginning of the string s and string q at the end of the strings.
Now he wonders, how many pairs of stringsp and q exists, such that the stringp + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo109 + 7.
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, 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 lengthm consisting of characters '(' and ')' only.
Print the number of pairs of string p andq such that p + s + q is a valid sequence of round brackets modulo109 + 7.
4 1(
4
4 4(())
1
4 3(((
0
In the first sample there are four different valid pairs:
- p = "(",q = "))"
- p = "()",q = ")"
- p = "", q = "())"
- 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.
分析:先对给定的串做括号匹配,然后求出剩下有l 个 ) 和 r 个 (,dp[i][j]表示长度为i的有效匹配序列中多出j个左括号的方案数,那么我们只要枚举p就能同时的到q.
#include <cstdio>#include <iostream>#define MAXN 1000000007using namespace std;int n,m,totl,totr;char c;long long dp[2005][2005],ans;int main(){scanf("%d %d",&n,&m);scanf("%c",&c);for(int i = 1;i <= m;i++) {scanf("%c",&c);if(c == '(') totl++;else if(totl) totl--; else totr++;}dp[0][0] = 1;for(int i = 1;i <= 2000;i++){dp[i][0] = dp[i-1][1];for(int j = 1;j <= i;j++) dp[i][j] = (dp[i-1][j-1] + dp[i-1][j+1]) % MAXN; }for(int i = 0;i <= n - m;i++) for(int j = totr;j <= i;j++) if(j - totr + totl <= n - m - i) ans = (ans + dp[i][j]*dp[n - m - i][j - totr + totl]) % MAXN;cout<<ans<<endl;}
- CODEFORCES 629 C Famil Door and Brackets
- Codeforces 629C Famil Door and Brackets
- CodeForces 629C Famil Door and Brackets
- CodeForces-629C-Famil Door and Brackets
- Codeforces 629C Famil Door and Brackets
- codeforces C. Famil Door and Brackets dp
- Codeforces 629C - Famil Door and Brackets dp
- codeforces 629C Famil Door and Brackets(dp)
- Codeforces 629C Famil Door and Brackets 【dp】
- Codeforces 629 C Famil Door and Brackets(dp)
- Codeforces 629C Famil Door and Brackets (DP)
- Codeforces 629C Famil Door and Brackets【dp】
- 【23.24%】【codeforces 629C】Famil Door and Brackets
- Codeforces 629C Famil Door and Brackets(dp)
- 【Codeforces 629 C Famil Door and Brackets 】+ dp
- CodeForces 629 C.Famil Door and Brackets(组合数学)
- 629C-Famil Door and Brackets
- codeforces #343 C. Famil Door and Brackets (dp)
- oracle知识点总结
- iOS开发Runtime那点事
- Android内存泄露排查
- JUNIT4 测试注意要点
- 排列序号
- CodeForces 629C Famil Door and Brackets
- iOS开发————数据本地化方式之SSKeychain
- String浅析
- Hive体系结构(四)注意事项与扩展特性
- sdk manager无法下载
- Problem F: LCS
- C++:建立一个被称为sroot()的函数,返回其参数的二次方根。重载sroot()3次,让它返回整数、长整数与双精度的二次方根
- 第十二周项目四 string类的运算符重载
- Properties是Hashtable的一个子类:以文件来进行存储键值对