UVALive 7454 Parentheses (栈+模拟)
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 (栈+模拟)的更多相关文章
- UVaLive 7454 Parentheses (水题,贪心)
题意:给定一个括号序列,改最少的括号,使得所有的括号匹配. 析:贪心,从左到右扫一下,然后统计一下左括号和右括号的数量,然后在统计中,如果有多了的右括号,那么就改成左括号,最后如果两括号数量不相等, ...
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- 【栈模拟dfs】Cells UVALive - 3486
题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- poj1363Rails(栈模拟)
主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...
- 【LintCode·容易】用栈模拟汉诺塔问题
用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...
- 51Nod 1289 大鱼吃小鱼 栈模拟 思路
1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...
- Code POJ - 1780(栈模拟dfs)
题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...
- HDOJ 4699 Editor 栈 模拟
用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
随机推荐
- 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
这里使用的是HttpComponents-Client-4.1.2 package com.jadyer.util; import java.io.File; import java.io.FileI ...
- shell/bash 交集、并集、差集
方法一(直接用文件名):取两个文本文件的并集.交集.差集并:sort -m <(sort file1 | uniq) <(sort file2 | uniq) | uniq交:sort - ...
- struct TABLE
struct TABLE { TABLE() {} /* Remove gcc warning */ TABLE_SHARE *s; handler *file; TABLE *next, *prev ...
- 利用序列化的方式实现C#深复制和浅复制
代码如下:具体看注释 [Serializable] public class A { public virtual string Name { get; set; } public int Age { ...
- sharepoint Linq方式的增,删,查,改
Site9527EntitiesDataContext (重要的类):连接实体与网站List操作 SPContext.Current.Web.Url:获取当前操作的页面 FirstOrDefault: ...
- bzoj3555: [Ctsc2014]企鹅QQ
将字符串hash.不难写.然而1.注意用longlong2.数组大小注意...3.似乎别人都用的unsigned long long ?. #include<cstdio> #includ ...
- Asp.Net生成RSS方法
一.RSS简介 什么是RSS? RSS是一种网页内容联合格式(web content sydication format). 它的名字是Really Simple Syndication的缩写. RS ...
- objective-c里的方法指针IMP的用法
SGPopSelectView.h @interface SGPopSelectView : UIView @property (nonatomic, assign) SEL selector; @p ...
- Java [Leetcode 303]Range Sum Query - Immutable
题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...
- (转)c & c++内存分配
一.预备知识—程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于 数据结构中的栈. ...