Parentheses

题目链接:

http://acm.hust.edu.cn/vjudge/contest/127401#problem/A

Description


http://7xjob4.com1.z0.glb.clouddn.com/9cf04e507e13a41d77bca3a75bb51e19

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


##题意:

括号匹配问题,求最少的翻转(左右括号转换)操作使得原串合法.


##题解:

栈模拟判断当前串是否合法:
①. 如果当前是'(',直接入栈.
②. 如果当前是')',如果栈非空,则弹出一个'('; 如果栈空就把当前的')'变成'('入栈.
最后的结果栈中肯定全是'(',那么把其中的一半变成')'即可.
与[HDU 5831](http://www.cnblogs.com/Sunshine-tcf/p/5761816.html)类似.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 1010
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

char str[maxn];

stack s;

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
while(t--)
{
int n; scanf("%d", &n);
while(!s.empty()) s.pop();
scanf("%s", str); int ans = 0;
for(int i=0; i<n; i++) {
if(str[i] == '(') {
s.push('(');
} else {
if(!s.empty()) s.pop();
else {
ans++;
s.push('(');
}
}
} ans += s.size() / 2; printf("%d\n", ans);
} return 0;

}

UVALive 7454 Parentheses (栈+模拟)的更多相关文章

  1. UVaLive 7454 Parentheses (水题,贪心)

    题意:给定一个括号序列,改最少的括号,使得所有的括号匹配. 析:贪心,从左到右扫一下,然后统计一下左括号和右括号的数量,然后在统计中,如果有多了的右括号,那么就改成左括号,最后如果两括号数量不相等, ...

  2. UVALive 3486/zoj 2615 Cells(栈模拟dfs)

    这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...

  3. 【栈模拟dfs】Cells UVALive - 3486

    题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...

  4. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  5. poj1363Rails(栈模拟)

    主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...

  6. 【LintCode·容易】用栈模拟汉诺塔问题

    用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...

  7. 51Nod 1289 大鱼吃小鱼 栈模拟 思路

    1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...

  8. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

  9. HDOJ 4699 Editor 栈 模拟

    用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

随机推荐

  1. Ubuntu中Eclipse安装与配置

    安装Eclipse: 第一种是通过Ubuntu自带的程序安装功能安装Eclipse,应用程序 ->Ubtuntu软件中心,搜Eclipse安装即可.第二种方法是用命令:应用程序->附件-& ...

  2. sql server 数据库 ' ' 附近有语法错误

    昨天做项目时候,遇到标题的问题,代码跟踪把sql 语句 复制出来在数据库执行不了, 然后重新写个一模一样的,然后在 赋值到代码中,还是同样的错误, 就是不知道哪里出现了错误,最后 把 sql 语句写成 ...

  3. MyBatis 实践 -动态SQL/关联查询

    MyBatis 实践 标签: Java与存储 动态SQL 动态SQL提供了对SQL语句的灵活操作,通过表达式进行判断,对SQL进行拼接/组装. if 对查询条件进行判断,如果输入参数不为空才进行查询条 ...

  4. Jqgrid入门-别具特色的Pager Bar (四)

    Pager Bar位于表格最下边.默认情况下,分为三部分.如图: 第一部分:导航按钮栏(Navigator) 第二部分:页码栏(Pager) 第三部分:记录信息栏(Record)         要实 ...

  5. Eclipse @override报错解决 必须覆盖超类方法

    解决办法:Windows->Preferences-->java->Compiler-->compiler compliance level设置成1.6

  6. crtbegin_dynamic.o: No such file: No such file or directory

    /homesec/android2/zhangbin/053work3/hi050src/HiSTBAndroidV400R001C00SPC050B012/prebuilt/linux-x86/to ...

  7. 【C#学习笔记】检测进程是否存在并关闭

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. 【C#学习笔记】Dictionary容器使用

    using System; using System.Collections.Generic; namespace ConsoleApplication { class Program { stati ...

  9. ffmepg 指定RTSP网络连接模式UDP还是TCP

    AVFormatContext *formatCtx = NULL; formatCtx = avformat_alloc_context(); AVDictionary* options = NUL ...

  10. cimge 这次能够图片大小尺寸

    CImage imSrc,imDest;imSrc.Load(……);//读入原始图片imDest.Create(……)//创建新大小的图片imSrc.StretchBlt(imDest.GetDC( ...