CF1095E.Almost Regular Bracket Sequence

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a bracket sequence ss consisting of nn opening '(' and closing ')' brackets.

A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.

You can change the type of some bracket sis_i. It means that if $s_i = $ ')' then you can change it to '(' and vice versa.

Your task is to calculate the number of positions ii such that if you change the type of the ii-th bracket, then the resulting bracket sequence becomes regular.

给你一个由 nn 个左括号 '(' 和右括号 ')' 组成的括号序列 ss。

合法括号序列(regular bracket sequence)是指:通过在原序列的字符之间插入字符 '1' 和 '+',可将其转化为一个正确的算术表达式。例如,括号序列 "()()" 和 "(())" 是合法的(对应表达式分别为 "(1)+(1)" 和 "((1+1)+1)"),而 ")(" 和 "(" 则不是。

你可以将某个位置 sis_i 上的括号类型进行翻转:即若 $s_i = $ ')',则可将其改为 '(';反之亦然。

你的任务是:计算满足如下条件的位置 ii 的个数——即若将第 ii 个括号的类型翻转,则所得括号序列成为合法括号序列。

输入格式

The first line of the input contains one integer nn (1≤n≤1061 \le n \le 10^6) — the length of the bracket sequence.

The second line of the input contains the string ss consisting of nn opening '(' and closing ')' brackets.

输入的第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6)——括号序列的长度。

输入的第二行包含一个由 nn 个左括号 '(' 和右括号 ')' 组成的字符串 ss。

输出格式

Print one integer — the number of positions ii such that if you change the type of the ii-th bracket, then the resulting bracket sequence becomes regular.

输出一个整数——即满足如下条件的位置 ii 的个数:若将第 ii 个括号的类型进行翻转,则所得括号序列变为合法序列。

输入输出样例

  • 输入#1

    6
    (((())

    输出#1

    3
  • 输入#2

    6
    ()()()

    输出#2

    0
  • 输入#3

    1
    )

    输出#3

    0
  • 输入#4

    8
    )))(((((

    输出#4

    0

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

首页