关闭

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

标签: acm模拟
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
(()))()(

 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网站的观点或立场

数据结构实验之排序四:寻找大富翁

Problem Description 2015胡润全球财富榜调查显示,个人资产在1000万以上的高净值人群达到200万人,假设给出N个人的个人资产值,请你快速找出排前M位的大富翁。 Inp...
  • Ameir_yang
  • Ameir_yang
  • 2016-12-08 16:48
  • 177

【LeetCode-面试算法经典-Java实现】【022-Generate Parentheses(生成括号)】

【022-Generate Parentheses(生成括号)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given n pairs of parentheses, ...
  • DERRANTCM
  • DERRANTCM
  • 2015-07-23 07:52
  • 2381

UVALive 4670(AC自动机)

题意:有n个串,输出在t串出现次数最多的那个串,如果有多个按照输入顺序输入 题解:模板多,长度短,文本串长。适合多模式串匹配,适用AC自动机,这里使用map映射stirng为...
  • qq_24489717
  • qq_24489717
  • 2016-02-26 18:14
  • 339

PHP环境时区设置成Asia/Chongqing导致的一个bug

得到某个时间戳所对应的日期的0时在这边的数据统计中经常用到,如得到1409144737(2014/8/27 21:5:37)的0时1409068800(2014/8/27 0:0:0),可用的方法也比...
  • orangeholic
  • orangeholic
  • 2014-09-03 11:28
  • 1108

UVALive 6837 (最小生成树)

题意是给定一个联通图,求这个图的最小生成树的不可替代边有哪些,并计算这些边的总权值. 先求出任意一棵MST,然后标记树边和非树边,然后枚举非树边,对于非树边的两个点u,v,在MST中如果u->v的路...
  • morejarphone
  • morejarphone
  • 2015-12-07 13:32
  • 277

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

Regionals 2015 >> Asia - Tehran >> 7530 - Cafebazaar 题目大意:一个公司有n个开发者,有m个APP可开发。其中一些开发者必选,一些APP必选。已知每...
  • loy_184548
  • loy_184548
  • 2016-08-02 20:31
  • 648

uvalive 4356 火势控制系统

在平面上有n个目标点,目标是找出一个圆心在(0,0)点处的扇形,至少覆盖其中的k个点,使得该扇形的面积最小 思路先枚举r再枚举角度 #include #include #include ...
  • u014664226
  • u014664226
  • 2015-06-15 21:34
  • 903

UVALive 7037 (最大密度子图 网络流)

每个数都看成一个节点,每个逆序对之间的节点连边,于是只需要求出这个图的最大密度子图,可以用最小割模型解决. #include #include #include #include #incl...
  • morejarphone
  • morejarphone
  • 2016-03-22 16:15
  • 478

UVALive 4126 (ac自动机做状态类)

题意: 给定最多10个长度不超过10的子串,让构造长度为n(n 分析: 暴力是26^25中可能性,那么怎样精简状态,可以定义d[ i ][ j ][ s ]为当前构造的串长为i且已经到达自动机j...
  • playwfun
  • playwfun
  • 2015-10-09 15:40
  • 329

UVALive 3983(单调队列优化dp)

链接:点击打开链接 题意:有n个垃圾,第i个垃圾坐标为(xi,yi),重量为wi,有一个机器人,要按照编号从小到大的顺序剑气所有的垃圾兵扔进垃圾桶,垃圾桶在源点,每次总重量不能超过C,两点间距离为曼...
  • stay_accept
  • stay_accept
  • 2016-11-20 16:00
  • 241
    博客通知
    访客统计
    Flag Counter
    个人资料
    • 访问:276715次
    • 积分:6118
    • 等级:
    • 排名:第4762名
    • 原创:318篇
    • 转载:13篇
    • 译文:0篇
    • 评论:393条
    博客专栏
    最新评论