CF917A.The Monster

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him.

Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.

A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally:

  • Empty string is a correct bracket sequence.
  • if s is a correct bracket sequence, then (s) is also a correct bracket sequence.
  • if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.

A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.

Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≤ l ≤ r ≤ |s| and the string s__l__s__l + 1... s__r is pretty, where s__i is i-th character of s.

Joyce doesn't know anything about bracket sequences, so she asked for your help.

由于威尔被困在颠倒世界中,他仍能通过圣诞彩灯与妈妈乔伊斯进行交流(他可以用意念控制彩灯的亮灭)。但他无法直接告诉妈妈自己的位置,因为将他掳至颠倒世界的怪物会察觉并再次转移他的位置。

因此,他设计了一个谜题来向妈妈传达自己的坐标。他的坐标即为以下问题的答案。

仅由括号 '(' 和 ')' 组成的字符串称为括号序列。其中某些括号序列被称为合法括号序列。更严格地定义如下:

  • 空字符串是一个合法括号序列;
  • 若 $ s $ 是一个合法括号序列,则 $ (s) $ 也是一个合法括号序列;
  • 若 $ s $ 和 $ t $ 均为合法括号序列,则 $ st $(即 $ s $ 与 $ t $ 的连接)也是一个合法括号序列。

由括号和问号 '?' 组成的字符串称为优美的,当且仅当存在一种方式,将每个问号替换为 '(' 或 ')',使得所得字符串是一个非空的合法括号序列。

威尔通过彩灯以摩尔斯电码的形式向妈妈发送了一个由括号和问号组成的字符串 $ s $,而他的坐标即为满足如下条件的整数对 $ (l,,r) $ 的个数:
$ 1 \leq l \leq r \leq |s| $,且子串 $ s_l s_{l+1} \dots s_r $ 是优美的(其中 $ s_i $ 表示字符串 $ s $ 的第 $ i $ 个字符)。

乔伊斯对括号序列一无所知,因此她请求你的帮助。

输入格式

The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≤ |s| ≤ 5000).

输入仅有一行,包含一个字符串 ss,该字符串仅由字符 '('、')' 和 '?' 组成(2 ≤ ∣s∣ ≤ 50002 \leq |s| \leq 5000)。

输出格式

Print the answer to Will's puzzle in the first and only line of output.

在输出的第一行且唯一一行中打印威尔谜题的答案。

输入输出样例

  • 输入#1

    ((?))

    输出#1

    4
  • 输入#2

    ??()??

    输出#2

    7

说明/提示

For the first sample testcase, the pretty substrings of s are:

  1. "(?" which can be transformed to "()".
  2. "?)" which can be transformed to "()".
  3. "((?)" which can be transformed to "(())".
  4. "(?))" which can be transformed to "(())".

For the second sample testcase, the pretty substrings of s are:

  1. "??" which can be transformed to "()".
  2. "()".
  3. "??()" which can be transformed to "()()".
  4. "?()?" which can be transformed to "(())".
  5. "??" which can be transformed to "()".
  6. "()??" which can be transformed to "()()".
  7. "??()??" which can be transformed to "()()()".

对于第一个样例测试用例,字符串 ss 的“漂亮”子串有:

  1. "(?",可转化为 "()"。
  2. "?)",可转化为 "()"。
  3. "((?)",可转化为 "(())"。
  4. "(?))",可转化为 "(())"。

对于第二个样例测试用例,字符串 ss 的“漂亮”子串有:

  1. "??",可转化为 "()"。
  2. "()"。
  3. "??()",可转化为 "()()"。
  4. "?()?",可转化为 "(())"。
  5. "??",可转化为 "()"。
  6. "()??",可转化为 "()()"。
  7. "??()??",可转化为 "()()()"。

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

首页