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排序方法sort的使用详解
对数组的排序: //对数组排序 public void arraySort(){ int[] arr = {1,4,6,333,8,2}; Arrays.sort(arr);//使用java.util ...
- Unity3D【新手问题】阴影效果不显示的原因
Unity 不显示阴影的原因: 模型尺寸太大了,镜头比较远 把模型缩小,镜头一定要拉到最近才能看到 这是我遇到的问题和解决方法, 另外一定要设置 Directional light 的 shadow ...
- 个人PE流程备忘
insmod memdisk linux16 /boot/memdisk iso raw initrd16 /iso/Win8PE.iso
- linux的基本命令
find: find [路径] [选项] [查找规则] [动作] -print 打印出找到的文件,以\n结束 -print0 打印出找到的文件,无换行 -exec command 对找到的文件执行命令 ...
- jsp页面根据当前时间和定义时间差计算动态倒计时
jsp页面根据当前时间和定义时间差计算动态倒计时http://www.jb51.net/article/74140.htm var maxtime =1000*60; //半个小时,按秒计算,自 ...
- java输出菱形
package test; public class Test { public static void main(String[] args) { for(int i=1;i<=4;i++){ ...
- 安卓平台多个视频叠加演示demo说明
多个音视频编辑演示说明: 第一个-----字幕和视频的叠加: 说明: 把字幕文件中的文字,按照时间叠加到视频上去,形成新的视频. 类似我们看电影时的字幕. 下载地址:http://www.cnblog ...
- Php 关于构造函数
子类在继承父类后,会拥有父类的属性和方法,这是继承的特性. 子类在构造函数会首先调用父类的构造函数来实例化父类的属性,然后调用子类的构造函数,一般你不写,并不表示没有调用,而是首先调用了父类的无参构造 ...
- Redis 使用 Eval 多个键值自增操作示例
在PHP上使用Redis 给多个键值进行自增,示例如下: $set['money'] = $this->redis->hIncrByFloat($key, $hour .'_money', ...
- FZU 2240 Daxia & Suneast's problem
博弈,$SG$函数,规律,线段树. 这个问题套路很明显,先找求出$SG$函数值是多少,然后异或起来,如果是$0$就后手赢,否则先手赢.修改操作和区间查询的话可以用线段树维护一下区间异或和. 数据那么大 ...