HDU 5831 Rikka with Parenthesis II
如果左括号数量和右括号数量不等,输出No
进行一次匹配,看匹配完之后栈中还有多少元素:
如果n=2,并且栈中无元素,说明是()的情况,输出No
如果n=2,并且栈中有元素,说明是)(的情况,输出Yes
如果n>2,并且栈中没有元素,或者有2个,或者有4个,输出Yes
如果n>2,并且栈中元素个数大于4个,输出No
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar(); int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=;
stack<int >s;
int T,n;
char t[maxn]; int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n); scanf("%s",t);
while(!s.empty()) s.pop();
int num1=,num2=;
if(n%==) printf("No\n");
else
{
for(int i=;t[i];i++)
{
if(t[i]=='(') num1++; else num2++;
if(s.empty())
{
if(t[i]=='(') s.push(-);
else s.push();
}
else
{
int num; if(t[i]=='(') num=-; else num=;
if(num==&&s.top()==-) s.pop();
else s.push(num);
}
}
if(num1!=num2) { printf("No\n"); continue; }
if(n==)
{
if(s.size()==) printf("No\n");
else printf("Yes\n");
}
else
{
if(s.size()==||s.size()==||s.size()==) printf("Yes\n");
else printf("No\n");
}
}
}
return ;
}
HDU 5831 Rikka with Parenthesis II的更多相关文章
- HDU 5831 Rikka with Parenthesis II(六花与括号II)
31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 5831 Rikka with Parenthesis II (栈+模拟)
Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- hdu 5831 Rikka with Parenthesis II 线段树
Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- HDU 5831 Rikka with Parenthesis II (贪心)
Rikka with Parenthesis II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- hdu 5831 Rikka with Parenthesis II 括号匹配+交换
Rikka with Parenthesis II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场
题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...
- HDU 5831 Rikka with Parenthesis II ——(括号匹配问题)
用一个temp变量,每次出现左括号,+1,右括号,-1:用ans来记录出现的最小的值,很显然最终temp不等于0或者ans比-2小都是不可以的.-2是可以的,因为:“))((”可以把最左边的和最右边的 ...
- 【HDU5831】Rikka with Parenthesis II(括号)
BUPT2017 wintertraining(16) #4 G HDU - 5831 题意 给定括号序列,问能否交换一对括号使得括号合法. 题解 注意()是No的情况. 任意时刻)不能比(超过2个以 ...
- HDU 5424——Rikka with Graph II——————【哈密顿路径】
Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
随机推荐
- Java多线程基础——线程间通信
在使用多线程的时候,经常需要多个线程进行协作来完成一件事情.在前面两章分析了Java多线程的基本使用以及利用synchronized来实现多个线程同步调用方法或者执行代码块.但上面两章的内容涉及到的例 ...
- Python random模块(获取随机数)常用方法和使用例子
random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用 ...
- Docker 搭建 etcd 集群及管理
环境 host1 10.1.99.13 host2 10.1.99.14 host3 10.1.99.15 host4 10.1.99.12(用于测试添加删除节点) 初始化集群 host1 $ doc ...
- Linux CentOS 7 YUM 安裝 MySQL 5.7
MySQL YUM 源:http://dev.mysql.com/downloads/repo/yum/ # 下載源 $ wget http://dev.mysql.com/get/mysql57-c ...
- cookie解决 未登录加入购物车 第一次访问弹出新手引导页面
浏览器携带cookie到服务器, 点击加入购物车-->后台检查-->是否登录(有没有sessionid) 没有登录--->secookie()返回给浏览器,把传递过来的商品id, 属 ...
- 负载均衡,最理想使用 redis实现session共享
负载均衡在多台php服务器负载均衡的情况下,第一秒请求是a服务器,第二秒请求是b服务器, session必须放在一个公共的服务器,最理想是使用 redis实现session共享.内存的速度比磁盘访问快 ...
- ecshop3.0.0注入
配个环境来演示给别人看..分析一下.flow.php文件缺陷,order_id在post请求没有单引号保护.造成注入 <?php elseif ($_REQUEST['step'] == 're ...
- feature2d相关
1.Harris角点检测 是基于灰度图像的角点检测. 灰度变化率函数如下: 其中的w(x,y)为加权函数,可为常数或为高斯函数.之后对E(u,v)进行泰勒级数的展开与化简,最终得到 ,,Ix,Iy是图 ...
- CODE[VS]-最小数和最大数-整数处理-天梯青铜
题目描述 Description 输入n个数,n<=100,找到其中最小的数和最大的数 输入描述 Input Description 第一行一个整数n 接下来一行n个整数,每个整数不超过231 ...
- THINKCMF-NGINX伪静态
location / { index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/index.php( ...