题目链接:

  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串满足合法串的状态,两者相乘。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef __int64 LL;
  5. const int INF = 1e9 + ;
  6. const int maxn = ;
  7. const int N = ;
  8. const int mod = 1e9+;
  9.  
  10. ///dp[i][j] 到第i个位置,有j个不平衡(括号
  11. LL dp[N][N], n, m;
  12. char str[maxn];
  13.  
  14. void init ()
  15. {
  16. int num = ;
  17. memset (dp, , sizeof(dp));
  18. dp[][] = ;
  19.  
  20. for (int i=; i<=num; i++)
  21. for (int j=; j<=i; j++)
  22. {
  23. if (j > )
  24. dp[i][j] = (dp[i][j] + dp[i-][j-]) % mod;
  25. dp[i][j] = (dp[i][j] + dp[i-][j+]) % mod;
  26. }
  27. }
  28.  
  29. LL solve ()
  30. {
  31. int minx = INF, tmp = , num = n - m;
  32.  
  33. for (int i=; str[i]; i++)
  34. {
  35. if (str[i] == '(')
  36. tmp ++;
  37. else
  38. tmp --;
  39. minx = min (minx, tmp);
  40. }
  41.  
  42. LL ans = ;
  43. for (int i=; i<=num; i++)
  44. for (int j=; j<=i; j++)
  45. if (j >= -minx && num-i >= j + tmp)
  46. ans = (ans + dp[i][j]*dp[num-i][j+tmp]) % mod;
  47.  
  48. return ans;
  49. }
  50.  
  51. int main ()
  52. {
  53. init ();
  54. while (scanf ("%I64d %I64d", &n, &m) != EOF)
  55. {
  56. scanf ("%s", str);
  57. printf ("%I64d\n", solve ());
  58. }
  59. return ;
  60. }

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. C#WinForm窗体监听/拦截操作动作

    C#中的事件也是通过封装系统消息来实现的,如果你在WndProc函数中不处理该消息 那么,它会被交给系统来处理该消息,系统便会通过代理来实现鼠标单击的处理函数,因此你可以通过 WndProc函数来拦截 ...

  2. openwrt 配置rsync服务

    一: rsyn简介 remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限.时间.软硬链接等附加信息. rsync是用 “rsyn ...

  3. easyUI datagrid组件能否有display:none的隐藏效果

    这个项目用了JQ easyUI datagrid 组件,我今天做了一个页面,页面有个div层,div里放了一个easyUI datagrid,页面初始化时div隐藏(display:none),通过点 ...

  4. mongo-spark 安装排故 ./sbt check

    [error] at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:) [error] at com.mong ...

  5. C# delegate Action<T> lambda表达式

    转载以记录:http://blog.csdn.net/educast/article/details/7219854 在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的 ...

  6. Delphi ActiveForm发布全攻略

    论坛上很多朋友(也包括我)提到ActiveForm的发布问题,都没有得到很好的解决.下面是本人开发ActiveForm的一点经验,拿出来跟大家分享,开发环境为 Win2000Server,IIS5.0 ...

  7. android adb 源码框架分析(1 系统)【转】

    本文转载自:http://blog.csdn.net/luansxx/article/details/25203269 ‘ Adb模块包括adb,adbd,源代码都在system/core/adb目录 ...

  8. MD5Util1

    package com.cc.hkjc.util; import java.math.BigInteger;import java.security.MessageDigest;import java ...

  9. ubuntu安装wine+plsql

    1.在ubuntu下装了win7的虚拟机,在使用plsql进行开发的时候发现很慢很卡,经常半天反应不过来.机器是不差的,1w5的thinkstation,实在受不了这种 速度,想着在ubuntu下搞一 ...

  10. 安卓开发eclipse如何导出项目

    安卓开发如何导出项目 方法/步骤   1 首先打开eclipse 2 选择file然后选择export 3 选择android application 4 点击next 5 选择项目browse可以更 ...