405人阅读 评论(0)

# 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;
}

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
博客通知
访客统计
个人资料
• 访问：276715次
• 积分：6118
• 等级：
• 排名：第4762名
• 原创：318篇
• 转载：13篇
• 译文：0篇
• 评论：393条
博客专栏
 Multi-University Training Contest 文章：37篇 阅读：37891
阅读排行
最新评论