hdu_5831_Rikka with Parenthesis II(模拟)
题目链接:hdu_5831_Rikka with Parenthesis II
题意:
给你一些括号的排列,必须交换一次,能不能将全部的括号匹配
题解:
模拟一下括号的匹配就行了,注意要特判只有一对括号是NO,因为必须要交换一次
#include <cstdio>
#include <cstring>
const int N = 1e5 +;
char s[N];
int st[N],top;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
scanf("%s", s);
if (n == && s[] == '(' && s[] == ')') {printf("No\n"); continue;}
top = ; st[] = -;
for (int i = ; i < n; i++)
{
if (s[i] == '(') st[++top] = ;
else if (st[top] == ) top--;
else st[++top] = ;
}
if (top == ) printf("Yes\n");
else if (top == && st[] == && st[] == ) printf("Yes\n");
else if (top == && st[] == && st[] == && st[] == && st[] == ) printf("Yes\n");
else printf("No\n");
}
return ;
}
hdu_5831_Rikka with Parenthesis II(模拟)的更多相关文章
- 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(六花与括号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 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 ...
- 汉诺塔问题II(模拟)
汉诺塔问题II Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1556 Solved: 720 Description 汉诺塔(又称河内塔)问题是源于 ...
- HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场
题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...
- HDU 5831 Rikka with Parenthesis II
如果左括号数量和右括号数量不等,输出No 进行一次匹配,看匹配完之后栈中还有多少元素: 如果n=2,并且栈中无元素,说明是()的情况,输出No 如果n=2,并且栈中有元素,说明是)(的情况,输出Yes ...
- 【HDU5831】Rikka with Parenthesis II(括号)
BUPT2017 wintertraining(16) #4 G HDU - 5831 题意 给定括号序列,问能否交换一对括号使得括号合法. 题解 注意()是No的情况. 任意时刻)不能比(超过2个以 ...
随机推荐
- HMM 前向后向算法(转)
最近研究NLP颇感兴趣,但由于比较懒,所以只好找来网上别人的比较好的博客,备份一下,也方便自己以后方便查找(其实,一般是不会再回过头来看的,嘿嘿 -_-!!) 代码自己重新写了一遍,所以就不把原文代码 ...
- CentOS6.5自带Python2.6.6升级至Python2.7
CentOS6.5中Python2.6升级到Python2.7 由于Python开发团队已不再支持2.6版本,且该版本对一些软件不支持,因此将2.6升级到2.7. 1.安装Python2.7: 下载源 ...
- 使用ObjectInputStream和ObjectOutputStream注意问题
1.对象序列化,类实现Serializable接口 不需要序列化的属性,使用transient声明 2.使用套接字流在主机之间传递对象注意问题: 学习自:Socket同时使用ObjectInputSt ...
- ajax客户端请求与服务端响应浅谈
AJAX,即Asynchronous Javascript And XML,AJAX本质是在HTTP协议的基础上以异步的方式与服务器进行通信. 所谓的异步,是指某段程序执行不会阻塞其他程序执行,其表现 ...
- 主题模型 利用gibbslda做数据集主题抽样
电子科技大学电子商务实验室Kai Yip,欢迎同行指正,也欢迎互相指导,学习. 广告打完,进入正题. 关于程序运行结果的分析请参照我的另一篇博客:http://www.cnblogs.com/nlp- ...
- Gs_Class.Gs_DataFunction数据操作类库20160225
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security ...
- iOS 调用私有函数安装app 卸载 app
1.环境 1.OS X EI Caption 10.11.1 & Xcode 7 2.Xcode安装Command Line Tools 3.iPhone 安装AppSync 2.Mobile ...
- 设计模式-GoF23
书呢,是2012年双11买的. 没有面向对象程序经验的人,果然还是看不懂的.
- 取消a标签在移动端点击时的背景颜色
一.取消a标签在移动端点击时的蓝色 -webkit-tap-highlight-color: rgba(255, 255, 255, 0); -webkit-user-select: none; -m ...
- Linux GIT服务器配置
Linux下安装git插件 1. 下载git网址: https://github.com/git/git/releases 2. 放入usr/src/下 3. tar -zxvf git-** ...