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 consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.
Output the maximum possible length of a regular bracket sequence.
(()))(
4
((()())
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;
}