Codeforces 629C Famil Door and Brackets DP
题意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000
这个长度为n的字符串要求有两个性质:1:就是任意前缀,左括号数量大于右括号数量
2:字符串中左括号的数量等于右括号
现在让你可以在长度为m的原串前加一个括号串p,在原串后加一个括号串q 最后p+m+q=n
问有多少种组合p,q能得到目标串
分析:(这题我不会,看了题解才会)
定义dp[i][j],为前缀长为i,且左括号数量-右括号数量=j的串有多少个
所以dp[0][0]=1;
if(j>0)dp[i][j]=dp[i-1][j-1]+dp[i-1][j+1]
else j==0 dp[i][j]=dp[i-1][j+1]
然后处理原串,找到 左-右的最终数量cnt,和最小数量d
然后枚举p的长度和平衡值 对于长度i, 当-d<=j时,p可以加到前面
然后当p确定后,q的长度也确定,因为最终 左=右 ,所以q 的(右-左)的代价也知道了
假设当前是i,平衡度是j,所以只要将dp[i][j]*dp[n-m-i][j+cnt]加到答案就行了
注意:dp[i][j]代表前缀i,平衡度为j的方案数, dp[n-m-i][j+cnt]为后缀n-m-i,平衡度为-(j+cnt)的方案数,是对称的,很重要
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 2e3+;
const LL mod = 1e9+;
LL dp[N][N];
char s[+];
int main()
{
int m,n;
scanf("%d%d%s",&n,&m,s+);
dp[][]=;
for(int i=; i<=n-m; ++i)
{
for(int j=; j<=i; ++j)
{
if(!j)
dp[i][j]=dp[i-][j+];
else
dp[i][j]=(dp[i-][j-]+dp[i-][j+])%mod;
}
}
int cnt=,d=m+;
for(int i=; i<=m; ++i)
{
if(s[i]=='(')++cnt;
else --cnt;
d=min(d,cnt);
}
LL ans=;
for(int i=; i<=n-m; ++i)
{
for(int j=; j<=i; ++j)
{
if(-d<=j&&cnt+j<=n-m-i)
ans=(ans+dp[i][j]*dp[n-m-i][cnt+j])%mod;
}
}
printf("%I64d\n",ans);
return ;
}
Codeforces 629C Famil Door and Brackets DP的更多相关文章
- codeforces 629C Famil Door and Brackets (dp + 枚举)
题目链接: codeforces 629C Famil Door and Brackets 题目描述: 给出完整的括号序列长度n,现在给出一个序列s长度为m.枚举串p,q,使得p+s+q是合法的括号串 ...
- Codeforces 629C Famil Door and Brackets(DP)
题目大概说给一个长m的括号序列s,要在其前面和后面添加括号使其变为合法的长度n的括号序列,p+s+q,问有几种方式.(合法的括号序列当且仅当左括号总数等于右括号总数且任何一个前缀左括号数大于等于右括号 ...
- CodeForces 629C Famil Door and Brackets
DP. 具体做法:dp[i][j]表示长度为 i 的括号串,前缀和(左括号表示1,右括号表示-1)为 j 的有几种. 状态转移很容易得到:dp[i][j]=dp[i - 1][j + 1]+dp[i ...
- 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 ...
- 【Codeforces629C】Famil Door and Brackets [DP]
Famil Door and Brackets Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Inp ...
- 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 ...
- 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 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
随机推荐
- velocity语法
1.声明变量 #set($var = XXX) 右边可以是以下的内容 Variable reference String literal Property reference Method refer ...
- C# Windows - TextBox 控件
.NET Framework内置了两个基本控件来提取用户输入的文本: TextBox和RichTextBox.这两个控件都派生于基类TextBoxBase,而TextBoxBase派生于Control ...
- Code for the Homework1 改进
#include <iostream> #include <vector> #include "shape.h" //using namespace std ...
- JS创建类和对象
JavaScript 创建类/对象的几种方式 在JS中,创建对象(Create Object)并不完全是我们时常说的创建类对象,JS中的对象强调的是一种复合类型,JS中创建对象及对对象的访问是极其灵活 ...
- [转载]MongoDB的$inc修改器
MongoDB的$inc修改器相当于编程语言中的 “+=”“$inc”只能用于操作数值类型的数据,包括整数.长整数和双精度浮点数,用于其他类型的数据会导致操作失败. >db.users.find ...
- [转载]c# 多线程一个带多个参数的方法
比如我要线程一个private void subPing(int pre,int end) 我在Thread t=之后应该如何写 用匿名委托吧!那么简单为什么要这样写!t = new Thread(d ...
- WCF 在VS中,添加服务引用,地址输入http://ip/Service.svc,点击前往,提示错误,内容如下:
WCF的service端的webconfig如下: <?xml version="1.0"?> <configuration> <system.ser ...
- spoj 95
栈应用 ...... 水题 #include<cstdio> #include<cstdlib> #include<cstring> #include<alg ...
- Spark 1.60的executor schedule
第一次看源码还是Spark 1.02.这次看新源码发现调度方式有了一些新的特征,在这里随便写一下. 不变的是,master还是接收Appclient和worker的消息,并且在接收RegisterAp ...
- thinkphp 减少文件目录
配置 'TMPL_FILE_DEPR'=>'_' 于是模板文件的格式为如:index_index.html,index_show.html .代替原来的目录结构:/index/index.htm ...