CSUOJ 1271 Brackets Sequence 括号匹配
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 括号匹配的更多相关文章
- POJ 1141 Brackets Sequence(括号匹配二)
题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...
- POJ 2955 Brackets --最大括号匹配,区间DP经典题
题意:给一段左右小.中括号串,求出这一串中最多有多少匹配的括号. 解法:此问题具有最优子结构,dp[i][j]表示i~j中最多匹配的括号,显然如果i,j是匹配的,那么dp[i][j] = dp[i+1 ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ-2955 Brackets(括号匹配问题)
题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: the empty sequence is a regular brac ...
- 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 ...
- Sereja and Brackets(括号匹配)
Description Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length ...
- UVA1626 - Brackets sequence(区间DP--括号匹配+递归打印)
题目描写叙述: 定义合法的括号序列例如以下: 1 空序列是一个合法的序列 2 假设S是合法的序列.则(S)和[S]也是合法的序列 3 假设A和B是合法的序列.则AB也是合法的序列 比如:以下的都是合法 ...
- Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)
题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...
- POJ 2955 Brackets(括号匹配一)
题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...
随机推荐
- SVN自动更新-win平台
把项目给外包做,他们天天整个ftp传来传去,上传一次还要到处翻View和Controller,还有漏传的情况,简直low到不行.看不下去了,就准备整个svn.虽然svn解决了上传的问题,但是自动发布还 ...
- 洛谷P3959 [NOIP2017]宝藏
[题目描述] 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋,也给出了这 n 个宝藏屋之间可供开发的 m 条道路和它们的长度. 小明决心亲自前往挖掘所有宝藏屋中的宝藏.但 ...
- bzoj 5055: 膜法师——树状数组
Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然,他能为长者所续的时间,为这三个维度上能量的乘 ...
- 一次非线上iowait高的情况的检查
一.现象 iowait高达30%.使用iotop查知jbd2/sda6-8占用60%的io写入.mongodb每秒写入达400k. 必然复现 二.排查 1.先检查是不是mongodb引起的 将mong ...
- 利用thrift rpc进行C++与Go的通信
一:什么是rpc rpc通俗来理解就是远程调用函数,相对于本地调用来说,只需要在主调函数中调用被掉函数即可,代码如下: void fun(int i) { cout << "fu ...
- msfpayload反弹shell
1.前期-- 情景就是当我们获得webshell时,我们想留下我们的后门,这个时候我们可以用到msfpayload与msfconsole结合使用 启动PostgreSQL服务:service post ...
- 【C++】数组-整数从大到小排序
1.实现过程 定义整型数组src,长度为10,初始化为{11,12,47,24,49,69,90,89,18,39}.之后用嵌套for循环比较相邻两个元素的大小,如果前一个元素大于后一个,不做任何操作 ...
- video标签、audio标签
1.video标签 <video src="nans.mp4" controls="controls" autoplay="autoplay& ...
- HDU 6214 Smallest Minimum Cut 最小割,权值编码
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这 ...
- MySQL 四种链接
1.内联接 INNER JOIN(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自然联接. 内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行. ...