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的更多相关文章

  1. codeforces 629C Famil Door and Brackets (dp + 枚举)

    题目链接: codeforces 629C Famil Door and Brackets 题目描述: 给出完整的括号序列长度n,现在给出一个序列s长度为m.枚举串p,q,使得p+s+q是合法的括号串 ...

  2. Codeforces 629C Famil Door and Brackets(DP)

    题目大概说给一个长m的括号序列s,要在其前面和后面添加括号使其变为合法的长度n的括号序列,p+s+q,问有几种方式.(合法的括号序列当且仅当左括号总数等于右括号总数且任何一个前缀左括号数大于等于右括号 ...

  3. Codeforces 629C Famil Door and Brackets DP

    题意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000 这个长度为n的字符串要求有两个性质:1:就是任意前缀,左括号数量大于 ...

  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. 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 ...

  7. 【23.24%】【codeforces 629C】Famil Door and Brackets

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. Codeforces Round #343 (Div. 2) C. Famil Door and Brackets

    题目链接: http://codeforces.com/contest/629/problem/C 题意: 长度为n的括号,已经知道的部分的长度为m,现在其前面和后面补充‘(',或')',使得其长度为 ...

  9. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. Break on _NSLockError() to debug.

    *** -[NSCondition dealloc]: condition (<NSCondition: 0x1039a450> '(null)') deallocated while s ...

  2. hdu Eddy's picture (最小生成树)

    Eddy's picture Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

  3. HDU 1054 Strategic Game 最小点覆盖

     最小点覆盖概念:选取最小的点数覆盖二分图中的所有边. 最小点覆盖 = 最大匹配数. 证明:首先假设我们求的最大匹配数为m,那么最小点覆盖必然 >= m,因为仅仅是这m条边就至少需要m个点.然后 ...

  4. HDU 1686 Oulipo(KMP+计算匹配成功次数)

    一开始总是超时,后来发现还是方法没找对,这个跟普通KMP不太一样的就是,KMP匹配成功的时候会完全跳过已经匹配成功的匹配段,至少我掌握的是.那么如何避免这样的问题呢,举个栗子啊 原串为ABABA,模式 ...

  5. Python基础学习3---数据结构

    数据结构 数据结构基本上就是---他们是可以处理数据的结构或者说他们是用来存储一组相关数据的. 在python中有三种内建的数据结构-----列表.元组和字典 列表(list) 列表就像是我们要去超市 ...

  6. Effective java -- 4 泛型

    第二十三条:请不要在代码中使用原生态类型就是像Set这种待泛型的,就把泛型明确写出来. 第二十四条:消除非受检警告就是Set<String> sets = new HashSet();这种 ...

  7. 为什么有时候必须添加sys.setdefaultencoding('utf-8')

    今天在尝试Python的CGI模块时遇到中文字符不能正确显示的问题,很郁闷.在网上仔细找了找,终于解决了这个问题,现在将解决方法陈述如下,以防下次失误. 页面源代码如下 #-*- coding: ut ...

  8. PHP字节格式化

    /** * 容量转换 * @param string $value 字节数值 * @return string */function GBKB($value) { $size = ($value &g ...

  9. 我也谈javascript闭包

    1.什么是闭包呢?Whenever you see the function keyword within another function, the inner function has acces ...

  10. FusionCharts生成报表应用

    1.需要组装要展示的数据,至于如何怎样去设计数据模型,看你要展示的图形和需要的数据就行了.来个简单的. 实体类,只有两个属性,也可以使用Bean里面的实体类,无所谓了. package com.gol ...