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 Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!
The sequence of round brackets is called valid if and only if:
the total number of opening brackets is equal to the total number of closing brackets;
for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.
Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.
Input
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string s of length m consisting of characters '(' and ')' only.
Output
Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.
Sample Input
4 1
(
Sample Output
4
Hint
题意
给你一个长度m的括号序列,然后让你补全成长度为n的合法括号序列
问你一共有多少种补全的方案
题解:
dp
dp[i][j]表示长度为i,当前平衡度为j的方案数是多少
然后暴力枚举ij就好了,左右乘法原则处理一下贡献
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2005;
const int mod = 1e9+7;
long long dp[maxn][maxn];//长度为i,平衡度为j的方案数
int n,m;
char s[100005];
int main()
{
dp[0][0]=1;
for(int i=1;i<=2000;i++)
{
dp[i][0]=dp[i-1][1];
for(int j=1;j<=i;j++)
dp[i][j]=(dp[i-1][j-1]+dp[i-1][j+1])%mod;
}
scanf("%d%d",&n,&m);
scanf("%s",s+1);
int sum=0,l=0,r=0;
for(int i=1;i<=m;i++)
{
if(s[i]=='(')sum++;
else sum--;
l=min(sum,l);
}
l=-l;
sum=0;
for(int i=m;i;i--)
{
if(s[i]==')')sum++;
else sum--;
r=min(sum,r);
}
r=-r;
long long ans = 0;
for(int i=0;i<=n-m;i++)
{
for(int j=0;j<=n-m;j++)
{
int len2 = n-m-i;
int j2 = j-sum;
if(j2<=n-m&&i>=l&&j2>=r)
ans=(ans+dp[i][j]*dp[len2][j2])%mod;
}
}
cout<<ans<<endl;
}
Codeforces Round #343 (Div. 2) C. Famil Door and Brackets dp的更多相关文章
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets
题目链接: http://codeforces.com/contest/629/problem/C 题意: 长度为n的括号,已经知道的部分的长度为m,现在其前面和后面补充‘(',或')',使得其长度为 ...
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads lca 树形dp
E. Famil Door and Roads 题目连接: http://www.codeforces.com/contest/629/problem/E Description Famil Door ...
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads
题目链接: http://www.codeforces.com/contest/629/problem/E 题解: 树形dp. siz[x]为x这颗子树的节点个数(包括x自己) dep[x]表示x这个 ...
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads (树形dp,lca)
Famil Door's City map looks like a tree (undirected connected acyclic graph) so other people call it ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- Codeforces Round #343 (Div. 2)
居然补完了 组合 A - Far Relative’s Birthday Cake import java.util.*; import java.io.*; public class Main { ...
- Codeforces Round #343 (Div. 2) B. Far Relative’s Problem 暴力
B. Far Relative's Problem 题目连接: http://www.codeforces.com/contest/629/problem/B Description Famil Do ...
- Codeforces Round #343 (Div. 2) B
B. Far Relative’s Problem time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #343 (Div. 2) A. Far Relative’s Birthday Cake 水题
A. Far Relative's Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/A Description Do ...
随机推荐
- Tornado 目录
第一章:引言 1.1 Tornado是什么? 1.1.1 Tornado入门 1.1.2 社区和支持 1.2 简单的Web服务 1.2.1 Hello Tornado 1.2.1.1 参数handle ...
- linux-open-source-development-tools【重点】
https://www.pluralsight.com/blog/software-development/linux-open-source-development-tools https://ww ...
- keil问题:Error: failed to execute 'C:\Keil\ARM\BIN\ArmCC'
1.打开cmd,进入相应的路径下 cd C:\Keil\ARM\BIN\ 输入 armcc 若显示如下界面则说明keil已经注册 2.若注册成功还没解决问题,则说明软件是在网上下载的破解版的,建议卸 ...
- MySQL建立高性能索引策略
索引永远是最好的查询解决方案嘛? 索引并不总是最好的工具.总的来说,只有当索引帮助存储引擎快速查找到记录带来的好处大于其带来的额外工作(比如插入操作后索引的维护)时,索引才是高效的. 对于非常小的表: ...
- C语言实现二叉排序树
程序以'#'结尾的二叉排序树. /*(双重指针 BSTree *T)问:数据结构中 二叉树建立结点为什么用 双重指针?详细解释下双重指针 答:指针的指针.因为树的结点要用指针描述.如果只用指针,作形参 ...
- poj 2104(线段树)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 45653 Accepted: 15177 Ca ...
- python 用abc模块构建抽象基类Abstract Base Classes
见代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/08/01 16:58 from abc import ABCMet ...
- AC日记——[HNOI2010]BOUNCE 弹飞绵羊 洛谷 P3203
[HNOI2010]BOUNCE 弹飞绵羊 思路: SBlct: 代码: #include <bits/stdc++.h> using namespace std; #define max ...
- 京东前端:PhantomJS 和NodeJS在网站前端监控平台的最佳实践
1. 为什么需要一个前端监控系统 通常在一个大型的 Web 项目中有很多监控系统,比如后端的服务 API 监控,接口存活.调用.延迟等监控,这些一般都用来监控后台接口数据层面的信息.而且对于大型网站系 ...
- Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))
A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...