CF629C.Famil Door and Brackets

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

随着 Famil Door 生日的临近,他的一些朋友(例如 Gabi)决定为他买一份礼物。他的朋友们打算购买一个由圆括号组成的字符串,因为 Famil Door 最喜欢长度为 nn 的括号字符串,胜过其他任何字符串!

当且仅当满足以下两个条件时,一个圆括号序列被称为合法的:

  1. 开括号的总数等于闭括号的总数;
  2. 对该序列的任意前缀,开括号的数量均大于或等于闭括号的数量。

Gabi 已经购买了一个长度为 mm(其中 m≤nm \leq n)的字符串 ss,并希望将其补全为一个长度为 nn 的合法括号序列。他将选择两个仅由圆括号组成的字符串 pp 和 qq,并将它们与 ss 拼接成字符串 p+s+qp + s + q,即把字符串 pp 添加到 ss 的开头,把字符串 qq 添加到 ss 的末尾。

现在他想知道:有多少对字符串 pp 和 qq,使得拼接后的字符串 p+s+qp + s + q 是一个合法的括号序列?由于该数目可能非常大,他希望计算结果对 109+710^9 + 7 取模的值。

输入格式

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.

第一行包含 nn 和 mm(1 ≤ m ≤ n ≤ 100 0001 ≤ m ≤ n ≤ 100\,000,且 n − m ≤ 2000n - m ≤ 2000)—— 分别表示目标字符串的长度,以及 Gabi 所购买的字符串的长度。

第二行包含一个长度为 mm 的字符串 ss,仅由字符 '(' 和 ')' 组成。

输出格式

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.

输出满足条件的字符串对 pp 和 qq 的数量,使得 p+s+qp + s + q 构成一个合法的圆括号序列,结果对 109+710^9 + 7 取模。

输入输出样例

  • 输入#1

    4 1
    (

    输出#1

    4
  • 输入#2

    4 4
    (())

    输出#2

    1
  • 输入#3

    4 3
    (((

    输出#3

    0

说明/提示

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. p="("p = \text{"("},q="))"q = \text{"))"}
  2. p="()"p = \text{"()"},q=")"q = \text{")"}
  3. p=""p = \text{""},q="())"q = \text{"())"}
  4. p=""p = \text{""},q=")()"q = \text{")()"}

在第二个样例中,唯一能得到目标字符串的方法是选择空串 pp 和空串 qq。

在第三个样例中,无法得到合法的括号序列。

输入解题思路,AI测评打分。不知道怎么写?

首页