关闭

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

标签: acm模拟
327人阅读 评论(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
(()))()(

 Sample Output

4
0
2

 Problem Idea

解题思路:

【题意】
问最少需要几次掉换('('->')' or ')'->'(')操作,使得所有括号均能匹配成功


【类型】
栈+模拟

【分析】
首先,大家应该都做过简单版的括号匹配问题

就是给你一个字符串,判断所有的括号是否均能匹配成功

对于这样的题,通常做法就是用栈来模拟,例如,"(()())))(((((())((",过程如下:





就这样进行一一匹配,直至处理完整个字符串


了解完此过程之后,我们回到该题上

显然,每次遇到')'时,如果栈中还有'(',两者肯定就进行匹配了

但栈中如果没有'('的话,')'就多出来,它不可能能够和它之后的任意字符匹配,所以与其最后才处理,不如我们此时就将该')'掉换,然后压入栈中,并记下已进行过一次掉换操作

这样最终多出来的都是后来输入的'(',将其中一半的'('掉换就可以使得所有括号成功匹配了

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

题目链接→UVALive Problem 7454 Parentheses

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#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网站的观点或立场
    博客通知
    个人资料
    • 访问:238017次
    • 积分:5732
    • 等级:
    • 排名:第4818名
    • 原创:318篇
    • 转载:13篇
    • 译文:0篇
    • 评论:393条
    博客专栏
    最新评论