题目链接:

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

    OpenWrt 启动时会执行 rc.d/ 下的脚本. 这篇文章 介绍了启动脚本里的规则. K50dropbear -> ../init.d/dropbear K85odhcpd -> .. ...

  2. HDU 6040 Hints of sd0061 nth_element函数

    Hints of sd0061 Problem Description sd0061, the legend of Beihang University ACM-ICPC Team, retired ...

  3. 对云资源服务商资源读写的架构思考:前端代码走token

    第一.统一了访问端接口.提高前端开发速度:第二统一了阿里各个产品的 数据读写模式: 第三,我们的服务器产生token时对读写规则做限制,特定的token由特定的规则产生,而不是让前端代代码去管控限制 ...

  4. ossfs常见配置错误

    以下问题出现在非root用户下 执行echo ××××> /etc/passwd-ossfs  bash: /etc/passwd-ossfs: Permission denied 使用sudo ...

  5. redis---01

    redis是什么: redis是开源,BSD许可,高级的key-value存储系统. 可以用来存储字符串,哈希结构,链表,集合,因此,常用来提供数据结构服务. redis和memcached相比,的独 ...

  6. mysql 数据库连接

    1.需要mysql驱动包:mysql-connector-java-5.1.7-bin.jar 2. package com.jmu.ccjoin.web.controller; import jav ...

  7. POJ2516 Minimum Cost —— 最小费用最大流

    题目链接:https://vjudge.net/problem/POJ-2516 Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Tota ...

  8. POJ2914 Minimum Cut —— 最小割

    题目链接:http://poj.org/problem?id=2914 Minimum Cut Time Limit: 10000MS   Memory Limit: 65536K Total Sub ...

  9. YTU 2419: C语言习题 等长字符串排序

    2419: C语言习题 等长字符串排序 时间限制: 1 Sec  内存限制: 128 MB 提交: 650  解决: 249 题目描述 在主函数中输入n(n<=10)个等长的字符串.用另一函数对 ...

  10. JavaScript模板引擎使用

    1. [代码]tmpl.js     // Simple JavaScript Templating// John Resig - http://ejohn.org/ - MIT Licensed(f ...