题目链接:

  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. (testng多个class文件执行时混乱,不是等一个class内的所有methods执行完再去执行下一个class内的内容)问题的解决

    问题描述如下: We use TestNG and Selenium WebDriver to test our web application. Now our problem is that we ...

  2. SKStoreReviewController之程序内评价

    在iOS 10.3出现之前,App实现评价方式一般有两种: (一)deep link调用.在app 链接地址后面拼上action=write-review这种方式可以实现程序内评价: (二)App跳转 ...

  3. Hadoop如何计算map数和reduce数

    阅读本文可以带着下面问题: 1.map和reduce的数量过多会导致什么情况? 2.Reduce可以通过什么设置来增加任务个数? 3.一个task的map数量由谁来决定? 4.一个task的reduc ...

  4. MapReduce算法形式六:只有Map独自作战

    案例六:Map独自直接输出 之前一直没有用过这个map独自输出的模式,就算是输出一些简单的我也会经过一次reduce输出,但是,发现这个map输出的结果跟我预想的有点不一样,我一直以为shuffle的 ...

  5. quilt

    1 什么是quilt quilt是一个patch管理工具,特别适合于对多个patch进行管理. quilt是基于gnu patch和diff的. 2 使用quilt创建一个patch 第一步,quil ...

  6. JAVA反射改动常量,以及其局限

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/barryhappy/article/details/24442953 问题,以及一个解决方式 今天公 ...

  7. struts2 过滤器

    Chain.doFilter的作用就是继续请求的传递,可传递给下一个filter也可传递给目标页面 如左侧传递给filter2,但fiter2使用上面或者下面的方法将倾情重定向到一个新的页面,而不再传 ...

  8. 域名ip自动跳转 跳转指定页面的js

    域名ip自动跳转 跳转指定页面的js 为了应对百度审核,需要客户的网站在个别地区跳转到另一个页面,就搞到了这段代码,屡试不爽,超实用.下面把地址换成你要访问的网站url地址或者文件url地址即可.超实 ...

  9. expect的安装与使用

    expect的安装与使用 是什么 expect 是用来进行自动化控制和测试的工具.主要是和交互式软件telnet ftp ssh 等进行自动化的交互. 如何安装 1.检测是否安装 ls /usr/bi ...

  10. codeforces 437B. The Child and Set 解题报告

    题目链接:http://codeforces.com/contest/437/problem/B 题目意思:给出两个整数 sum 和 limit,问能否从1 - limit 这些数中选出一些数(注意: ...