codeforces629C Famil Door and Brackets (dp)
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:
- 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 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 109 + 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 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 109 + 7.
4 1
(
4
4 4
(())
1
4 3
(((
0
题意:给你一个长度为n的括号匹配串(不一定恰好匹配),让你在这个串的前面和后面加上一些括号匹配串,使得这个括号串平衡(平衡的含义是对于任意位置的括号前缀和大于等于0,且最后的前缀和为0)。
思路:比较容易想到的思路是枚举这个字符串前面p字符串的长度,那么后面q字符串的长度就知道了。那么p字符串要满足什么条件呢,因为要使得任意位置的前缀和大于等于0,所以我们可以使得p字符串的前缀和大于等于字符串s的最小前缀和minx,那么p+s就符合前缀和大于等于0,然后q的方案数也能确定了。我们用dp[i][j]表示i个括号平衡度为j的方案数,那么可以先预处理出来dp的值。然后我们算出s字符串的最小前缀和minx,最后我们只要枚举p的长度和平衡度c,那么sum+=dp[p][c]*dp[n-m-p][now+c],(now是整个s字符串的平衡度,考虑q的方案数时,我们要考虑对称性)。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define eps 1e-15
#define maxn 100050
#define MOD 1000000007
char s[maxn];
ll dp[2050][2060];
int main()
{
int n,m,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
scanf("%s",s+1);
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(i=1;i<=n-m;i++){
for(j=0;j<=i;j++){
if(j>0){
dp[i][j]=(dp[i][j]+dp[i-1][j-1])%MOD;
}
dp[i][j]=(dp[i][j]+dp[i-1][j+1])%MOD;
if(dp[i][j]>=MOD)dp[i][j]-=MOD;
}
}
int minx=inf;
int now=0;
for(i=1;i<=m;i++){
if(s[i]=='(')now++;
else now--;
minx=min(minx,now);
}
ll sum=0;
for(i=0;i<=n-m;i++){
for(j=0;j<=i;j++){
if(j>=-minx && j+now<=n-m-i){
sum=(sum+dp[i][j]*dp[n-m-i][j+now ])%MOD;
}
}
}
printf("%I64d\n",sum);
}
return 0;
}
codeforces629C Famil Door and Brackets (dp)的更多相关文章
- 【Codeforces629C】Famil Door and Brackets [DP]
Famil Door and Brackets Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Inp ...
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets dp
C. Famil Door and Brackets 题目连接: http://www.codeforces.com/contest/629/problem/C Description As Fami ...
- codeforces 629C Famil Door and Brackets (dp + 枚举)
题目链接: codeforces 629C Famil Door and Brackets 题目描述: 给出完整的括号序列长度n,现在给出一个序列s长度为m.枚举串p,q,使得p+s+q是合法的括号串 ...
- codeforces629C Famil Door and Brackets (dp)
题意:给你一个长度为n的括号匹配串(不一定恰好匹配),让你在这个串的前面加p串和后面加上q串,使得这个括号串平衡(平衡的含义是对于任意位置的括号前缀和大于等于0,且最后的前缀和为0). 思路:枚举这个 ...
- Codeforces 629C Famil Door and Brackets DP
题意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000 这个长度为n的字符串要求有两个性质:1:就是任意前缀,左括号数量大于 ...
- Codeforces629 C. Famil Door and Brackets
C. Famil Door and Brackets time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces 629C Famil Door and Brackets(DP)
题目大概说给一个长m的括号序列s,要在其前面和后面添加括号使其变为合法的长度n的括号序列,p+s+q,问有几种方式.(合法的括号序列当且仅当左括号总数等于右括号总数且任何一个前缀左括号数大于等于右括号 ...
- 【23.24%】【codeforces 629C】Famil Door and Brackets
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- ZOJ 2604 Little Brackets DP
DP: 边界条件:dp[0][j] = 1 递推公式:dp[i][j] = sum{dp[i-k][j] * dp[k-1][j-1] | 0<k≤i} i对括号深度不超过j的,能够唯一表示为( ...
随机推荐
- 【Java基础】面向对象下
面向对象下 这一章主要涉及其他关键字,包括 this.super.static.final.abstract.interface.package.import 等. static 在 Java 类中, ...
- servlet+jsp完成简单登录
将用户在注册界面中的数据填充到数据库相对应的表格中.当用户再次登录时,从数据库中拿到相应的数据查询并与页面的数据做对比,判断是否登陆成功. 需要在HTML文件中将form表单上的action属性值设置 ...
- Count PAT's (25) PAT甲级真题
题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...
- 【SpringBoot】Spring Boot 集成SwaggerAPI
Spring Boot 集成SwaggerAPI 文章目录 Spring Boot 集成SwaggerAPI Swagger 添加依赖 配置类 config 控制类 controller 接口测试 页 ...
- stat filename
查看文件的mtime,atime,ctime 3个时间
- YYDS: Webpack Plugin开发
目录 导读 一.cdn常规使用 二.开发一个webpack plugin 三.cdn优化插件实现 1.创建一个具名 JavaScript 函数(使用ES6的class实现) 2.在它的原型上定义 ap ...
- dblink查找对应的目标端session
v$session试图中process字段代表的是客户端所在机器的进程号 例如我使用toad连接数据库,查询到的process即toad的进程号 SELECT process FROM V$SESSI ...
- SuperUpdate.sh 一键更换Linux软件源脚本
一.前言 有时候会遇到 Linux 的源更新速度非常的缓慢,特别是在国内使用默认的源,因为国内的网络环境,经常会出现无法更新,更新缓慢的情况.在这种情况下,更换一个更适合或者说更近,更快的软件源,会为 ...
- Hmailserver搭建邮箱服务器
由于阿里云,谷歌云,腾讯云等服务器都不开放25端口和pop3端口,想要使用邮箱服务得购买他们的企业邮箱,但是对于个人而言比较贵. 所以我们需要利用家庭宽带申请公网IP. 首先打电话给运营商客服,申请动 ...
- SELECT ... FOR UPDATE or SELECT ... FOR SHARE Locking Reads session
小结: 1.注意使用限制 Locking reads are only possible when autocommit is disabled (either by beginning transa ...