CSU 1271: Brackets Sequence(数学啊 )
来源:互联网 发布:apple mac mini 编辑:程序博客网 时间:2024/04/30 12:51
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1271
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
1373
HINT
Source
中南大学第七届大学生程序设计竞赛
问能有多少种方法,使字符串的括号完整!
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int num[100017];char s[100017];int main(){ int t; int n; scanf("%d",&t); while(t--) { scanf("%s",s); int len = strlen(s); int tt = 0; for(int i = 0; i < len; i++) { if(s[i]=='(') num[i] = 1; if(s[i]==')') num[i] = -1; tt+=num[i]; } int sum = 0; int ans = 0; if(tt == 1)//缺右括号 { for(int i = 0; i < len; i++) { sum+=num[i]; //printf("sum:%d\n",sum); if(sum > 0) ans++; else if(sum == 0) ans=0; } } else if(tt == -1)//缺左括号 { for(int i = len-1; i >= 0; i--) { sum+=num[i]; //printf("sum:%d\n",sum); if(sum < 0) ans++; else if(sum == 0) ans=0; } } printf("%d\n",ans); } return 0;}
- CSU 1271: Brackets Sequence(数学啊 )
- Brackets Sequence(记搜)
- 1626 - Brackets sequence(DP)
- Brackets Sequence (poj 1141)
- Brackets Sequence (区间dp)
- Brackets Sequence (POJ-1141)
- CSU 1407: 最短距离(数学啊 )
- CSU 1410: 整数转换(数学啊 )
- CSU 1265: Dice (数学啊 )
- CSU 1270: Swap Digits(数学啊 )
- Brackets Sequence
- Brackets Sequence
- Brackets sequence
- Brackets Sequence
- Brackets Sequence
- Brackets Sequence
- csu 1759 Triangle(数学)
- POJ 1141 Brackets Sequence(区间DP)
- IDbHelper接口NewTableHandler实现增强
- ubuntu 换镜像源(下载由慢变快)
- android控件开发之ListView
- windows下Apache2.2.17+2个tomcat6.0.20集群(新手)
- 开发问题集
- CSU 1271: Brackets Sequence(数学啊 )
- MySQL性能优化的最佳20+条经验
- 数据库行转列的sql语句 (抛砖引玉)
- iBATIS创建操作
- codeforces 219a k重复字符串。
- 十种排序算法(java描述)
- Bootstrap3简单好用,轻松实现手机适配
- 在ubuntu下安装ulipad
- iBATIS读取操作