codeforces 1095E

给定一个由括号组成的序列,你需要找出改变多少个括号类型能使序列变为正规序列。正规序列可以通过插入字符'1'和'+'在原有字符之间变成正确的算术表达式。题目提供了输入输出示例,并指出只有在栈内有且仅有两个相同字符时才进行计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. Almost Regular Bracket Sequence

time limit per test 3 seconds

memory limit per test 256 megabytes

 

You are given a bracket sequence s consisting of n 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 si. It means that if si= ')' then you can change it to '(' and vice versa.

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

Input

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

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

Output

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

Examples

input

6
(((())

output

3

input

6
()()()

output

0

input

1
)

output

0

input

8
)))(((((

output

0

 

当且仅当栈内有且仅有两个相同字符时进行计算,对左右括号分情况讨论向不同方向计数
 

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
    stack< pair<char,int> > input;
    char save[n];
    char temp;
    int Count=0;
    for(int i=0;i<n;i++){
        cin>>temp;
        save[i]=temp;
        if(temp==')' && input.size()!=0 && input.top().first=='(') input.pop();
        else input.push(make_pair(temp,i));
    }
    if(input.size()==0){
        cout<<0;
        return 0;
    }
    pair<char,int> after =input.top();
    input.pop();
    int work=0;
    if(input.size()!=1 || after.first!=input.top().first){
        cout<<0;
        return 0;
    }
    else if(after.first=='('){
        for(int i=after.second;i<n;i++)
            if(save[i]=='(') work++;
    }
    else{
        for(int i=input.top().second;i>=0;i--)
            if(save[i]==')') work++;
    }
    cout<<work;
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值