C. Bracket Sequence Deletion

Problem - C - Codeforces

C. Bracket Sequence Deletion

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — 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≤n≤5⋅105) — 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⋅105 (∑n≤5⋅105∑n≤5⋅105).

Output

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.

Example

input

Copy

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

output

Copy

1 0
1 1
2 0
1 0
1 1
import sys
import math
#sys.stdin = open("in.txt", "r")
 
T = int(input())
for _ in range(T):
    n= int(input())
    s=input()
    #print(s)
    l,cnt=0,0
    while l+1<n:
        if s[l]=='(' or (s[l]==')' and s[l+1]==')'):
            l+=2
        else:
            r=l+1
            while r<n and s[r]!=')':
                r+=1
            if r==n:break
            l=r+1
        cnt+=1
    print('{} {}'.format(cnt,n-l))

  思维题,需要考虑清楚情况

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值