CF1657C.Bracket Sequence Deletion

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a bracket sequence consisting of nn characters '(' and/or )'. You perform several operations with it.

During one operation, you choose the shortest prefix of this string (some amount of first characters of the string) that is good and remove it from the string.

The prefix is considered good if one of the following two conditions is satisfied:

  • this prefix is a regular bracket sequence;
  • this prefix is a palindrome of length at least two.

A bracket sequence is called regular if it is possible to obtain a correct arithmetic expression by inserting characters '+' and '1' into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not.

The bracket sequence is called palindrome if it reads the same back and forth. For example, the bracket sequences )), (( and )(() are palindromes, while bracket sequences (), )( and ))( are not palindromes.

You stop performing the operations when it's not possible to find a good prefix. Your task is to find the number of operations you will perform on the given string and the number of remaining characters in the string.

You have to answer tt independent test cases.

给你一个由 nn 个字符 '(' 和/或 ')' 组成的括号序列。你需要对该序列执行若干次操作。

每次操作中,你需选择该字符串的最短前缀(即字符串开头的若干连续字符),该前缀必须是“好”的,并将它从字符串中移除。

当满足以下两个条件之一时,该前缀被称为“好”的:

  • 该前缀是一个合法括号序列;
  • 该前缀是一个长度至少为 2 的回文串。

若能通过在该括号序列中插入字符 '+' 和 '1' 得到一个合法的算术表达式,则称该括号序列为合法括号序列。例如,序列 (())()、() 和 (()(())) 是合法的,而 )(、(() 和 (()))( 则不是。

若一个括号序列正读与反读完全相同,则称其为回文串。例如,括号序列 ))、( ( 和 )(() 是回文串,而 ()、)( 和 ))( 则不是回文串。

当无法再找到“好”的前缀时,操作停止。你的任务是:对给定字符串,求出总共执行的操作次数以及最终剩余的字符数。

你需要回答 tt 个相互独立的测试用例。

输入格式

The first line of the input contains one integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. The next 2t2t lines describe test cases.

The first line of the test case contains one integer nn (1≤n≤5⋅1051 \le n \le 5 \cdot 10^5) — the length of the bracket sequence.

The second line of the test case contains nn characters '(' and/or ')' — the bracket sequence itself.

It is guaranteed that the sum of nn over all test cases do not exceed 5⋅1055 \cdot 10^5 (∑n≤5⋅105\sum n \le 5 \cdot 10^5).

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。接下来的 2t2t 行描述各个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤5⋅1051 \le n \le 5 \cdot 10^5),表示括号序列的长度。

每个测试用例的第二行包含 nn 个字符,每个字符为 '(' 或 ')',即括号序列本身。

保证所有测试用例的 nn 值之和不超过 5⋅1055 \cdot 10^5(即 ∑n≤5⋅105\sum n \le 5 \cdot 10^5)。

输出格式

For each test case, print two integers cc and rr — the number of operations you will perform on the given bracket sequence and the number of characters that remain in the string after performing all operations.

对于每个测试用例,输出两个整数 cc 和 rr —— 分别表示对给定括号序列执行的操作次数,以及执行所有操作后字符串中剩余的字符数量。

输入输出样例

  • 输入#1

    5
    2
    ()
    3
    ())
    4
    ((((
    5
    )((()
    6
    )((()(

    输出#1

    1 0
    1 1
    2 0
    1 0
    1 1

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

首页