# UVALive Problem 7454 Parentheses

Accept: 0    Submit: 0
Time Limit: 3.000 seconds

## Problem Description

A bracket is a punctuation mark, which is used in matched pairs, usually used within articles or programs. Brackets include round brackets, square brackets, curly brackets, angle brackets, and various other pairs of symbols. Let’s focus on the round brackets, also called parentheses.

A sequence of parentheses is said to be well-formed if the parentheses are properly nested. For example, A = a1a2 . . . a18 = “(()())()()()(())()” is well-formed, but B = b1b2 . . . b18 = “(()())))(((((())((” is not. (See Figure 1.) More formally, a sequence of parentheses P = p1p2 . . . pn is well-formed if

(1) when scanning it from p1 to pn, the number of right parentheses does not exceed the number of left parentheses at any state, and

(2) the numbers of left and right parentheses are equal.

Figure 1. Two sequences of parentheses.

AutoText is a company, which is developing a text editor for programmers. The new editor will provide many powerful functions to automatically correct typing errors. On a keyboard, the left and right parentheses are adjacent. Thus, it is often that “)” is mistyped as “(” or vice versa. And therefore, one of the functions AutoText wants to provide is to automatically convert a sequence of parentheses P (that may not be well-formed) into a wellformed sequence P'. In the conversion, the only allowed operation is to reverse a parenthesis (i.e., either to replace a “(” with a “)” or to replace a “)” with a “(”). For example, in Figure 1, we can convert B into the well-formed sequence A by performing 4 reverse operations on b7, b10, b12, b18 . Of course, there may be several ways to convert a sequence into a well-formed sequence. A conversion is optimal if it uses the minimum number of reverse operations.

Please write a program to compute the minimum number of reverse operations that make a given sequence of parentheses P = p1p2 . . . pn well-formed.

## Input

The first line contains an integer T ≤ 10 indicating the number of test cases. The first line of each test case contains an even integer n, 2 ≤ n ≤ 100, indicating the length of P. Next, the second line gives the sequence P.

## Output

For each test case, output the minimum number of reverse operations that make P well-formed.

## Sample Input

3
18
(()())))(((((())((
2
()
8
(()))()(

4
0
2

【题意】

【类型】

【分析】

【时间复杂度&&优化】
O(n)

## Source Code

/*Sherlock and Watson and Adler*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 105;
const int M = 100005;
const int inf = 1000000007;
const int mod = 7;
stack<char> s;
char ch[N];
int main()
{
int t,n,i,k,ans;
scanf("%d",&t);
while(t--)
{
k=ans=0;//k记录未匹配的左括号个数
while(!s.empty())
s.pop();
scanf("%d",&n);
scanf("%s",ch);
for(i=0;i<n;i++)
if(ch[i]=='(')
s.push('('),k++;
else if(!s.empty()&&s.top()=='(')
s.pop(),k--;
else
s.push('('),ans++,k++;
printf("%d\n",ans+=k/2);
}
return 0;
}

#### UVALive Problem 7457 Discrete Logarithm Problem——Regionals 2015 :: Asia - Taipei

2016-08-29 10:18:11

#### UVALive Problem 7456 Least Crucial Node——Regionals 2015 :: Asia - Taipei

2016-08-29 22:19:43

#### Regionals 2015 >> Asia - Tehran >> 7530 - Cafebazaar【二分图最佳匹配】【最小费用可行流】

2016-08-02 20:31:02

#### PHP5中时差相差8小时问题的解决

2008-09-01 21:43:00

#### UVALive 7454 Parentheses

2016-08-17 18:36:18

#### Regionals 2015 :: Asia - Taipei 部分题解

2016-08-28 10:38:05

#### Regionals 2013 Asia - Daejeon （部分题目题解）

2015-05-02 08:57:33

#### UVALive - 7454 Parentheses 栈应用

2017-10-28 18:18:09

#### UVALive 4260 Fortune Card Game （Regionals 2008 Asia Taipei +DP）

2015-10-14 21:23:57

#### Regionals 2015 :: Asia - Taipei B Linear Ecosystem 高斯消元（真模板）

2016-08-31 21:21:39

## 不良信息举报

UVALive Problem 7454 Parentheses——Regionals 2015 :: Asia - Taipei