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]的最大括号匹配对数. 则得到状态转移方 ...
随机推荐
- Header File Dependencies
[Header File Dependencies] 什么时候可以用前置声明替代include? 1.当 declare/define pointer&reference 时. 2.当 dec ...
- nginx.conf 基础配置
### 全局块开始### #配置允许运行nginx服务器的用户和用户组 user nobody; #配置允许nginx进程生成的worker process 数 worker_processes 1; ...
- C++模拟OC的多重自动释放池
使用过OC的都知道,OC的引用计数机制用起来还比较方便.于是就仿照OC的形式搞了个C++引用计数. 支持多重自动释放池,每次autorelease都会放到栈顶的自动释放池中. 自动释放池也可以像变量一 ...
- BZOJ4816 数字表格
4816: [Sdoi2017]数字表格 Time Limit: 50 Sec Memory Limit: 128 MB Description Doris刚刚学习了fibonacci数列.用f[i ...
- NameValuePair方式传参数
今天工作中联调外部的一个接口用post方式传输,我按照文档封装参数成Jason字符串传入,但是对方一直接受参数为空,折腾了半天也没找到问题.很苦恼,检查代码都没有错误,可是为什么对方接受参数为空呢?然 ...
- python变量内存地址释放与加速并行计算多线程
1.导入numba和gc包进行并行计算和内存释放 代码如下很容易的: #coding:utf-8 import time from numba import jit, prange, vectoriz ...
- 关于项目中根据当前数据库中最大ID生成下一个ID问题——(五)
1.关于部门管理时候根据上级产生下级部门ID的问题(传入一个参数是上级部门id)
- 连接数据库及出现System.AccessViolationException错误的解决方法
调试后发现, connection.Open();以后报错,System.AccessViolationException: 尝试读取或写入受保护的内存.这通常指示其他内存已损坏,网上搜了很多都没有作 ...
- Callable和futrue、ExecutorService的用法
首先说明是为了解决什么问题? 为了解决主线程无谓等待浪费服务器资源的问题.当主线程执行一个费时的操作时,比如客户端发起一个请求,该请求在服务器端处理很复杂,如需要调用其他系统的接口,总之比较耗时.这时 ...
- sicily 1500. Prime Gap
Description The sequence of n ? 1 consecutive composite numbers (positive integers that are not prim ...