Description

Let us define a regular brackets sequence in the following way:

1. Empty sequence is a regular sequence.

2. If S is a regular sequence, then (S) is a regular sequence.

3. If A and B are regular sequences, then AB is a regular sequence.

For example, these sequences of characters are regular brackets sequences: (), (()), ()(), ()(()), ((())())().

And all the following character sequences are not: (, ), ((), ()), ())(, (()(, ()))().

A sequence of characters '(' and ')' is given. You can insert only one '(' or ')' into the left of the sequence, the right of the sequence, or the place between any two adjacent characters, to try changing this sequence to a regular brackets sequence.

Input

The first line has a integer T (1 <= T <= 200), means there are T test cases in total.

For each test case, there is a sequence of characters '(' and ')' in one line. The length of the sequence is in range [1, 105].

Output

For each test case, print how many places there are, into which you insert a '(' or ')', can change the sequence to a regular brackets sequence.

What's more, you can assume there has at least one such place.

Sample Input

4
)
())
(()(())
((())())(()

Sample Output

1
3
7
3

Hint

题意:给定一个括号字符串,插入一个括号使得所有的括号匹配,问有多少处可以插入括号

思路:定义( 的值为1 ) 的值为-1,用以数组记录每个位置的值,遇( +1 遇 )-1,当第一次出现-1时,则前面的所有位置都可以加括号

#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
string s;
int cal[100010];
int main()
{
int T;
while (cin >> T)
{
while (T--)
{
memset(cal, 0, sizeof(cal));
cin >> s;
int sum = 0;
for (int i = 0; i < s.length(); i++)
{
int pre;
if (i == 0)
pre = 0;
else
pre = i - 1;
if (s[i] == '(')
cal[i] = cal[pre] + 1;
else if (s[i] == ')')
cal[i] = cal[pre] - 1;
}
for (int i = 0; i < s.length(); i++)
{
if (cal[i] == -1)
{
sum = sum + i + 1;
break;
}
}
                        //反着再来一遍    
for (int i = s.length() - 1; i >= 0; i--)
{
if (s[i] == '(')
cal[i] = cal[i+1] + 1;
else if (s[i] == ')')
cal[i] = cal[i+1] - 1;
}
for (int i = s.length() - 1; i >= 0; i--)
{
if (cal[i] == 1)
{
sum = sum + s.length() - i;
break;
}
}
cout << sum << endl;
}
}
return 0;
}
/**********************************************************************
Problem: 1271
User: leo6033
Language: C++
Result: AC
Time:152 ms
Memory:2728 kb
**********************************************************************/

CSUOJ 1271 Brackets Sequence 括号匹配的更多相关文章

  1. POJ 1141 Brackets Sequence(括号匹配二)

    题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...

  2. POJ 2955 Brackets --最大括号匹配,区间DP经典题

    题意:给一段左右小.中括号串,求出这一串中最多有多少匹配的括号. 解法:此问题具有最优子结构,dp[i][j]表示i~j中最多匹配的括号,显然如果i,j是匹配的,那么dp[i][j] = dp[i+1 ...

  3. poj 2955 Brackets (区间dp 括号匹配)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  4. POJ-2955 Brackets(括号匹配问题)

    题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: the empty sequence is a regular brac ...

  5. C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)

    冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...

  6. Sereja and Brackets(括号匹配)

    Description Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length  ...

  7. UVA1626 - Brackets sequence(区间DP--括号匹配+递归打印)

    题目描写叙述: 定义合法的括号序列例如以下: 1 空序列是一个合法的序列 2 假设S是合法的序列.则(S)和[S]也是合法的序列 3 假设A和B是合法的序列.则AB也是合法的序列 比如:以下的都是合法 ...

  8. Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)

    题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...

  9. POJ 2955 Brackets(括号匹配一)

    题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...

随机推荐

  1. iScroll使用方法

    最基本的iScroll使用结构 <div id="wrapper"> <ul> <li></li> ..... </ul> ...

  2. spring框架学习(八)spring管理事务方式之注解配置

    1.DAO AccountDao.java package cn.mf.dao; public interface AccountDao { //加钱 void increaseMoney(Integ ...

  3. 深入浅出JavaScript之跨域总结

    什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...

  4. 用Emacs看电影

    大多数人用emacs听歌,我却喜欢用emacs看电影.用 EMMS 和 mplayer 结合,看电影真是太方便了. 不要从源里安装EMMS,它可能给你安装别的播放器,没必要,我们有 mplayer 足 ...

  5. python概念-各类绑定的概念和property的变态一面

    # 编辑者:闫龙 # 1.什么是绑定到对象的方法,如何定义,如何调用,给谁用?有什么特性 #在类中定义的(self)方法都是绑定到对象的方法 #定义 class a: def b(self):#绑定到 ...

  6. 差分约束系统+(矩阵)思维(H - THE MATRIX PROBLEM HDU - 3666 )

    题目链接:https://cn.vjudge.net/contest/276233#problem/H 题目大意:对于给定的矩阵  每一行除以ai  每一列除以bi 之后 数组的所有元素都还在那个L- ...

  7. js原生选择class DOM元素

    document.querySelector(".demo"); querySelector() 方法返回匹配指定选择器的第一个元素.如果需要返回所有的元素,使用 querySel ...

  8. Count of Smaller Number before itself

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  9. C#里partial关键字的作用

    1. 什么是局部类型?C# 2.0 引入了局部类型的概念.局部类型允许我们将一个类.结构或接口分成几个部分,分别实现在几个不同的.cs文件中.局部类型适用于以下情况: (1) 类型特别大,不宜放在一个 ...

  10. 【前端开发】限制input输入保留两位小数

    <input type="text" name='amount' id="cash_num" placeholder="请输入金额" ...