题目链接:

  codeforces 629C Famil Door and Brackets

题目描述:

  给出完整的括号序列长度n,现在给出一个序列s长度为m。枚举串p,q,使得p+s+q是合法的括号串,长度为n,问p,q的取值数目。

解题思路:

  枚举p的长度,可以直接得到q的长度。因为要满足在任意位置'(' 数目大于 ’)‘ 的数目,所以统计一下s串中 ’(‘ - ')' 的最小数目minx。dp[i][j] 代表 到位置 i 时,满足 '(' - ')' == j 的情况数目。然后枚举p的 j (i > -minx), 然后根据p串推出q串满足合法串的状态,两者相乘。

 #include <bits/stdc++.h>
using namespace std; typedef __int64 LL;
const int INF = 1e9 + ;
const int maxn = ;
const int N = ;
const int mod = 1e9+; ///dp[i][j] 到第i个位置,有j个不平衡(括号
LL dp[N][N], n, m;
char str[maxn]; void init ()
{
int num = ;
memset (dp, , sizeof(dp));
dp[][] = ; for (int i=; i<=num; i++)
for (int j=; j<=i; j++)
{
if (j > )
dp[i][j] = (dp[i][j] + dp[i-][j-]) % mod;
dp[i][j] = (dp[i][j] + dp[i-][j+]) % mod;
}
} LL solve ()
{
int minx = INF, tmp = , num = n - m; for (int i=; str[i]; i++)
{
if (str[i] == '(')
tmp ++;
else
tmp --;
minx = min (minx, tmp);
} LL ans = ;
for (int i=; i<=num; i++)
for (int j=; j<=i; j++)
if (j >= -minx && num-i >= j + tmp)
ans = (ans + dp[i][j]*dp[num-i][j+tmp]) % mod; return ans;
} int main ()
{
init ();
while (scanf ("%I64d %I64d", &n, &m) != EOF)
{
scanf ("%s", str);
printf ("%I64d\n", solve ());
}
return ;
}

codeforces 629C Famil Door and Brackets (dp + 枚举)的更多相关文章

  1. Codeforces 629C Famil Door and Brackets DP

    题意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000 这个长度为n的字符串要求有两个性质:1:就是任意前缀,左括号数量大于 ...

  2. Codeforces 629C Famil Door and Brackets(DP)

    题目大概说给一个长m的括号序列s,要在其前面和后面添加括号使其变为合法的长度n的括号序列,p+s+q,问有几种方式.(合法的括号序列当且仅当左括号总数等于右括号总数且任何一个前缀左括号数大于等于右括号 ...

  3. CodeForces 629C Famil Door and Brackets

    DP. 具体做法:dp[i][j]表示长度为 i 的括号串,前缀和(左括号表示1,右括号表示-1)为 j 的有几种. 状态转移很容易得到:dp[i][j]=dp[i - 1][j + 1]+dp[i ...

  4. 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 ...

  5. 【Codeforces629C】Famil Door and Brackets [DP]

    Famil Door and Brackets Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Inp ...

  6. 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 ...

  7. 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 ...

  8. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

  9. codeforces 21D. Traveling Graph 状压dp

    题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...

随机推荐

  1. (转)gcc学习笔记

    1.gcc -Wall hello.c -o hello //编译源文件,显示警告信息 2../a.out   //运行程序 3.gcc -Wall calc.c /usr/lib/libm.a -o ...

  2. Linux环境下如何查找哪个线程使用CPU最长

    top -H -p pid 查看端口是否被占用: netstat -apn|grep 80

  3. Android研究之游戏开发摄像头更新

     游戏中摄像头的原理介绍        在游戏开发中更新摄像头的位置能够决定屏幕显示的内容,尤其是RPG类游戏摄像头有着很关键的数据.我举一个样例 有时候我们在玩RPG游戏的时候进入一个新的场景 ...

  4. 【献给CWNU的师弟】Web篇

    2014年10月8日 献给CWNU的师弟

  5. http权威指南(一)-Http概述

    Http概述 在Web中,不管是浏览器还是server都是通过Http相互通信的.那么Http是怎样工作的呢? 首先,client向server发送Http请求,server会在Http响应中回送所请 ...

  6. 在Windows上使用libcurl发起HTTP请求

    curl下载地址https://curl.haxx.se/download.html 当前最新版本为7.61.0 将下载的curl-7.61.0.zip解压,得到curl-7.61.0 在目录curl ...

  7. IOS中调用系统拨打电话发送短信

    一.调用打电话界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat ...

  8. SpringBoot发送简单文本邮件

    1.pom.xml添加 spring-boot-starter-mail 依赖 <dependency> <groupId>org.springframework.boot&l ...

  9. 一步一步学Silverlight 2系列文章

    概述 由TerryLee编写的<Silverlight 2完美征程>一书,已经上市,在该系列文章的基础上补充了大量的内容,敬请关注.官方网站:http://www.dotneteye.cn ...

  10. position属性中的绝对定位和相对定位

    absolute(绝对定位):1.如果没有父级DIV,则会根据浏览器原始点去定位,而且跟他相邻的DIV会忽略它,定位后则可用TRBL(top,right,bottom,left)去布局.注意:TRBL ...