codeforces 629C Famil Door and Brackets (dp + 枚举)
题目链接:
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 + 枚举)的更多相关文章
- Codeforces 629C Famil Door and Brackets DP
题意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000 这个长度为n的字符串要求有两个性质:1:就是任意前缀,左括号数量大于 ...
- 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 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- codeforces 21D. Traveling Graph 状压dp
题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...
随机推荐
- C#WinForm窗体监听/拦截操作动作
C#中的事件也是通过封装系统消息来实现的,如果你在WndProc函数中不处理该消息 那么,它会被交给系统来处理该消息,系统便会通过代理来实现鼠标单击的处理函数,因此你可以通过 WndProc函数来拦截 ...
- openwrt 配置rsync服务
一: rsyn简介 remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限.时间.软硬链接等附加信息. rsync是用 “rsyn ...
- easyUI datagrid组件能否有display:none的隐藏效果
这个项目用了JQ easyUI datagrid 组件,我今天做了一个页面,页面有个div层,div里放了一个easyUI datagrid,页面初始化时div隐藏(display:none),通过点 ...
- mongo-spark 安装排故 ./sbt check
[error] at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:) [error] at com.mong ...
- C# delegate Action<T> lambda表达式
转载以记录:http://blog.csdn.net/educast/article/details/7219854 在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的 ...
- Delphi ActiveForm发布全攻略
论坛上很多朋友(也包括我)提到ActiveForm的发布问题,都没有得到很好的解决.下面是本人开发ActiveForm的一点经验,拿出来跟大家分享,开发环境为 Win2000Server,IIS5.0 ...
- android adb 源码框架分析(1 系统)【转】
本文转载自:http://blog.csdn.net/luansxx/article/details/25203269 ‘ Adb模块包括adb,adbd,源代码都在system/core/adb目录 ...
- MD5Util1
package com.cc.hkjc.util; import java.math.BigInteger;import java.security.MessageDigest;import java ...
- ubuntu安装wine+plsql
1.在ubuntu下装了win7的虚拟机,在使用plsql进行开发的时候发现很慢很卡,经常半天反应不过来.机器是不差的,1w5的thinkstation,实在受不了这种 速度,想着在ubuntu下搞一 ...
- 安卓开发eclipse如何导出项目
安卓开发如何导出项目 方法/步骤 1 首先打开eclipse 2 选择file然后选择export 3 选择android application 4 点击next 5 选择项目browse可以更 ...