CF_26B_RegularBracketSequence

Regular Bracket Sequence
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?

Input

Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.

Output

Output the maximum possible length of a regular bracket sequence.

Sample test(s)
input
(()))(
output
4
input
((()())
output
6

最近一直在做dp,尤其是刚做区间dp

结果想什么都在dp

这个题目明显不需要,而且数据量也不允许dp

因为括号只有一种,只需要简单的刷过去就可以了……

看代码长度就可以知道一切了……

//如此简单的问题,居然看成动态规划
//为此卡了好长时间!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

const int M=1e6+5;
char s[M];
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        int len=strlen(s);
        int l=0,r=0;
        for(int i=0;i<len;i++)
        {
            if(s[i]=='(')
               l++;
            if(s[i]==')'&&l>0)
            {
                l--;
                r++;
            }
        }
        printf("%d\n",r*2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值