CodeForces 629C Famil Door and Brackets
DP。
具体做法:dp[i][j]表示长度为 i 的括号串,前缀和(左括号表示1,右括号表示-1)为 j 的有几种。
状态转移很容易得到:dp[i][j]=dp[i - 1][j + 1]+dp[i - 1][j - 1],表示 i 这位分别放上右括号和左括号。
然后就是要处理题目的问题了:
我们可以枚举P串的长度和P串的前缀和,来看这种情况下是否符合题目要求,如果符合答案增加。
那么如何判断P串长度为left,前缀和为p的情况下,有几种符合题目要求呢?
先对已经存在的那个S串做一次括号匹配,做完之后剩下的肯定是 L个右括号+R个左括号
接下来的过程都是从左往右看P串,从右往左看Q串
我们假设P串的前缀和是p,S串的前缀和是k,很容易得到k=R-L;我们根据p和k可以推出Q串的前缀和是p+k(注意:Q串是从右往左看的)
我们需要保证p+k>=0&&p+k<=n-m,此外还需要保证 P串的前缀和 - L >=0,即p - L >= 0,Q串前缀和 - R>=0,即p + k - R >= 0
满足上述几个条件,答案增加 dp[left][p] * dp[right][p + k]
题外话:卡特兰数第n项,就是dp[2*n][0]。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = + ;
char s[maxn];
char st[maxn];
int top;
long long dp[ + ][ + ];
long long MOD = 1e9 + ;
int n, m;
long long ans;
long long k; void read()
{
scanf("%d%d", &n, &m);
scanf("%s", s);
} void init()
{
memset(dp, , sizeof dp);
memset(st, , sizeof st);
ans = ; k = ; top = -;
} void work()
{
for (int i = ; s[i]; i++)
{
if (top == -) st[++top] = s[i];
else
{
if (st[top] == '('&&s[i] == ')') st[top] = , top--;
else st[++top] = s[i];
}
} int L = , R = ;
for (int i = ; st[i]; i++)
{
if (st[i] == ')') L++;
else break;
}
R = strlen(st) - L;
k = R - L; dp[][] = ; for (int i = ; i <= n - m; i++)
{
for (int j = ; j <= n - m; j++)
{
if (j + <= n - m) dp[i][j] = (dp[i][j] + dp[i - ][j + ]) % MOD;
if (j - >= ) dp[i][j] = (dp[i][j] + dp[i - ][j - ]) % MOD;
}
} for (int left = ; left <= n - m; left++)
{
int right = n - m - left; for (int p = ; p <= left; p++)
{
if (p + k >= && p - L >= && p + k - R >= && p + k <= n - m)
ans = (ans + (dp[left][p] * dp[right][p + k]) % MOD) % MOD;
}
} printf("%lld\n", ans); } int main()
{
read();
init();
work();
return ;
}
CodeForces 629C Famil Door and Brackets的更多相关文章
- codeforces 629C Famil Door and Brackets (dp + 枚举)
题目链接: codeforces 629C Famil Door and Brackets 题目描述: 给出完整的括号序列长度n,现在给出一个序列s长度为m.枚举串p,q,使得p+s+q是合法的括号串 ...
- Codeforces 629C Famil Door and Brackets(DP)
题目大概说给一个长m的括号序列s,要在其前面和后面添加括号使其变为合法的长度n的括号序列,p+s+q,问有几种方式.(合法的括号序列当且仅当左括号总数等于右括号总数且任何一个前缀左括号数大于等于右括号 ...
- Codeforces 629C Famil Door and Brackets DP
题意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000 这个长度为n的字符串要求有两个性质:1:就是任意前缀,左括号数量大于 ...
- 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 ...
- 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 ...
- 【23.24%】【codeforces 629C】Famil Door and Brackets
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets
题目链接: http://codeforces.com/contest/629/problem/C 题意: 长度为n的括号,已经知道的部分的长度为m,现在其前面和后面补充‘(',或')',使得其长度为 ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- VS2013使用技巧汇总
1. Peek View 在不新建TAB的情况下快速查看.编辑一个函数的代码. 以前要看一个函数的实现,需要在使用的地方点击F12跳转到该函数,实际上这是很浪费时间的.VS2013Peek View便 ...
- Python 3.5.1 Syntax & APIs(Continue Updating..
print(x, end=' ') instead of print(x) to escape the default line-changing-output. print(str.ljust(si ...
- Mac系统Git生成ssh公钥
Mac系统Git生成ssh公钥 在使用Git仓库进行代码管理时,新的电脑上往往需要生成ssh公钥进行匹配,Mac系统生成Git公钥过程如下: 1.检查本机是否已有公钥 在终端中输入如下命令: ? 1 ...
- Gson解析数据
package com.bwie.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputS ...
- HTML元素分类:块级元素 内联元素和内联块状元素
在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 1,块状元素 常用的块状元素有: <div>.<p>.<h1 ...
- 转博客至github
呃呃呃,当初是从新浪博客转过来的,现在发现github的静态博客对我来说用起来更方便. 转至github,这里的东西以后有空会一点一点移过去. http://jcf94.github.io
- MapReduce入门例子
计算文档中不同单词的个数. hello you hello me 步骤如下:
- 【项目笔记】【bug】数组空指针异常
package com.example.googleplay.ui.holder; import java.util.ArrayList; import android.view.View; impo ...
- 白话解释IIS并发连接数
做负载均衡的时候会发现有很多并发数的参数.其中有一个"IIS并发连接数"是我们要关注的. 假设"IIS并发连接数"显示为1000,这并不代表有1000个客户端在 ...
- UIView(包括子类)的几个初始化时执行动作的时机
转载自:http://www.tqcto.com/article/mobile/56963.html 根据你需要执行的动作, 这里有几个方法: -(id)initWithFrame:(CGRect)f ...