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 ...
随机推荐
- 树莓派开启smb
1.安装smb apt-get install samba samba-common-bin 2.修改/etc/samba/smb.conf配置 设置使用系统用户登入 增加smb访问文件夹 [shar ...
- Linux 入门记录:十三、Linux 扩展权限
一.默认权限 每一个终端都有一个 umask 属性,是用来确定新建文件或目录的默认权限的“掩码”(mask 有“掩码”的含义,至于 u,后面说). Linux 中一般有默认的权限掩码,使用命令 uma ...
- appium===登陆应用的案例
import time import os from appium import webdriver from selenium.webdriver.support.ui import WebDriv ...
- 移动端测试===安卓设备共享程序-发布版本“share device”
分享一个开源的项目 share device 项目地址:https://github.com/sunshine4me/ShareDevicePublish/tree/win7-x64 首先选择对应系统 ...
- Memcached内存缓存技术
Memcached是什么,有什么作用? Memcached是一个开源的.高性能的内存缓存软件,从名称上看Mem就是内存的意思,而Cache就是缓存的意思. Memcached通过在事先规划好的内存空间 ...
- IE11中实现颜色渐变
background: -ms-linear-gradient(left,#daa23e,#ad7f27); 下面是css3中颜色渐变对各个浏览器的写法:background: -webkit-lin ...
- mysql大法
mysql大法 MySQL 安装方式 1.rpm(yum) 2.源码包 3.通用二进制 企业中版本选择 5.6 5.7 选择 GA 6个月到1年之间的------------------------- ...
- shell读取nginx配置文件中nginx的端口
#!/bin/shport=`nl /usr/local/openresty/nginx/conf/nginx.conf | sed -n '/listen/p' | awk 'NR==1{print ...
- JQuery 获取页面某一元素的位置
获取页面某一元素的绝对X,Y坐标 var X = $('#ElementID').offset().top; var Y = $('#ElementID').offset().left; 获取相对(父 ...
- ubuntu上安装mysql及导入导出
ubuntu上安装mysql: 1. sudo apt-get install mysql-server 2. apt-get isntall mysql-client3. sudo apt-get ...