5637: Regular Bracket Sequence
时间限制: 1 Sec 内存限制: 128 MB提交: 44 解决: 27
[ 提交][ 状态][ 讨论版]
题目描述
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?
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
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e6+10;
char s[maxn];
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",2*r);
}
return 0;
}