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. java 去除末尾的零 如果小数点可以去除同时去除小数点

    String s; if(s.indexOf(".") > 0){ //正则表达 s = s.replaceAll("0+?$", "" ...

  2. 【清华集训 2017】小Y的地铁 [模拟退火]

    小Y的地铁 Time Limit: 50 Sec  Memory Limit: 256 MB Description Input Output 对于每组输入数据,输出一行一个整数,表示除掉这 n 个换 ...

  3. 博主退役了qwq

    noip靠太差的(蒟蒻)博主退役了qwq 感觉以后都没什么机会可以继续写博客了 这个博客八成是坟了呀qwq 其实感觉也没有什么人关注qwq 所以也不长篇大论些什么了 就这样吧qwq

  4. Linux dig命令

    dig(Domain Information Groper),和nslookup作用有些类似,都是DNS查询工具 1.dig命令格式 dig @dnsserver name querytype 如果你 ...

  5. $("节点名").html("字符串")和$("节点名").text("字符串")区别

    1. 经过html方法: $(".js_info").html("~!`@#$%^& ";'<>\=/-!·#¥%…&*()—+|` ...

  6. windebug常用命令

    使用~查看所有线程 切换到一号线程:~1s 查看所有线程的托管堆栈  ~* e!clrstack 怎么查看,当前线程下,变量的信息? 对于托管代码而言,最核心的命令就是!do(dump object的 ...

  7. java 面试题总结(一)

    从网上找了些面试题,自己手工总结了理解了一下,如有理解错误,还请指正. java基础 1.String 为什么是final的?     https://www.zhihu.com/question/3 ...

  8. 【Android开发】之Fragment开发1

    一直知道Fragment很强大,但是一直都没有去学习,现在有些空闲的时间,所以就去学习了一下Fragment的简单入门.我也会把自己的学习过程写下来,如果有什么不足的地方希望大牛指正,共同进步! 一. ...

  9. log4j与commons-logging slf4j的关系

    1. slf4j     他只提供一个核心slf4j api(就是slf4j-api.jar包),这个包只有日志的接口并没有实现     所以如果要使用就得再给它提供一个实现了些接口的日志包,     ...

  10. 使用mongoose操作mongodb数据库

    1.如何启动mongodb数据库 参考地址:http://www.runoob.com/mongodb/mongodb-window-install.html 在数据库安装的地方,bin文件夹,输入 ...