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 ...
随机推荐
- 【BZOJ 1085】 [SCOI2005]骑士精神
Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2, ...
- 在Hadoop分布式文件系统的索引和搜索
FROM:http://www.drdobbs.com/parallel/indexing-and-searching-on-a-hadoop-distr/226300241?pgno=3 在今天的信 ...
- git - 必备指令
1. 查看远程分支 加上-a参数可以查看远程分支,远程分支会用红色表示出来(如果你开了颜色支持的话): $ git branch -a master remote tungway v1. * zron ...
- 【扩展欧几里得】Codevs 1200: [noip2012]同余方程
Description 求关于 x 同余方程 ax ≡ 1 (mod b)的最小正整数解. Input Description 输入只有一行,包含两个正整数 a, b,用 一个 空格隔开. Outpu ...
- 【莫比乌斯反演】关于Mobius反演与gcd的一些关系与问题简化(bzoj 2301 Problem b&&bzoj 2820 YY的GCD&&BZOJ 3529 数表)
首先我们来看一道题 BZOJ 2301 Problem b Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd( ...
- smarty 时间格式化date_format
代码如下:$smarty = new Smarty; $smarty->assign('yesterday', strtotime('-1 day')); $smarty->display ...
- SQL Server CONVERT() 函数
http://www.w3school.com.cn/sql/func_convert.asp 定义和用法 CONVERT() 函数是把日期转换为新数据类型的通用函数. CONVERT() 函数可以用 ...
- jquery layout学习
1.官网:http://layout.jquery-dev.com/index.cfm 2.博客园:http://www.cnblogs.com/chen-fan/articles/2044556.h ...
- RedMine项目管理系统邮件推送设置(Windows环境)
RedMine项目管理系统有邮箱推送功能,当Bug,安全漏洞等内容被修改.解决.评论的时候,系统会通过邮件 及时的通知你的团队和客户.邮件通知的环节.形式.时间.接受人均可定制,功能十分实用. 下面是 ...
- pmf,cpmf,pdf,cdf,iid的解释