Codeforces 629C Famil Door and Brackets 【dp】

本文探讨了如何通过添加合适的括号序列使一个给定的括号字符串变为有效括号序列的问题。具体地,给定了一个长度为m的括号序列s和目标长度n,需找到所有可能的前缀p和后缀q,使得p + s + q构成一个有效的括号序列且总长度为n。文章提供了一种动态规划方法来解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Famil Door and Brackets
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. the total number of opening brackets is equal to the total number of closing brackets;
  2. 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 strings 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.

Examples
input
4 1
(
output
4
input
4 4
(())
output
1
input
4 3
(((
output
0
Note

In the first sample there are four different valid pairs:

  1. p = "(", q = "))"
  2. p = "()", q = ")"
  3. p = "", q = "())"
  4. p = "", q = ")()"

In the second sample the only way to obtain a desired string is choose empty p and q.

In the third sample there is no way to get a valid sequence of brackets.



定义一个合法串:

(1),'('的总数 == ')'的总数。

(2), 串中所有前缀 满足 '('的个数 >= ')'的个数。

题意:给定一个合法串的长度n和一个长度为m的中间串s,要求你找到一个p串和q串,使得p + s + q是一个合法串且长度为n。问有多少种不同的方案数。 结果 % (1e9+7)。


思路:设置dp[i][j]表示长度为i的合法串且'('的个数 - ')'的个数 == j的方案数。

我们称j为该串的平衡度。其实dp[i][j] = dp[i][-j],因为'('和')'可以互换。

转移有dp[i][j] = dp[i-1][j+1] + dp[i-1][j-1]。

那么我们可以枚举p串的长度和平衡度,找到合法的q串。再统计方案数。

注意:我们不能仅仅要求p + s + q合法就够了,必须保证选出的p + s也是一个合法串。

假设p + s长度为i且平衡度为j(j>=0)。那么q串长度为n-m-i,平衡度为-j。

AC代码;


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#include <stack>
using namespace std;
typedef long long LL;
const int MAXN = 2005;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
void add(LL &x, LL y) {x += y, x %= MOD;}
LL dp[MAXN][MAXN];
void getdp()
{
    memset(dp, 0LL, sizeof(dp)); dp[0][0] = 1LL;
    for(int i = 1; i <= 2000; i++)
    {
        dp[i][0] = dp[i-1][1];
        for(int j = 1; j <= i; j++)
            add(dp[i][j], (dp[i-1][j+1]+dp[i-1][j-1]) % MOD);
    }
}
int main()
{
    getdp();
    int n, m; cin >> n >> m;
    string str; cin >> str;
    stack<int> S; int sum = 0;
    int Min = INF;
    for(int i = 0; i < m; i++)
    {
        if(str[i] == '(') sum++;
        else sum--;
        Min = min(Min, sum);
    }
    int len = n - m; LL ans = 0LL;
    for(int i = 0; i <= len; i++)
    {
        for(int j = 0; j <= i; j++)
        {
            if(j + Min < 0 || len - i < j + sum) continue;
            add(ans, dp[i][j] * dp[len-i][j+sum] % MOD);
        }
    }
    cout << ans << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值