Rikka with Parenthesis II---hdu5831(括号匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5831
给你一个只包含‘(’‘)’的字符串,然后让我们交换两个字符一次,问是否能得到一个合法的匹配;必须要交换一次,而且只能交换一次;
() No
))(( Yes这两个是比较特殊的注意一下;
可以把串中的第一个')'和最后一个'('交换一下,然后判断是否合法即可;
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stack>
using namespace std; #define INF 0xfffffff
#define N 100050
typedef long long LL; int n;
char s[N]; int Judge()
{
stack<char>sta; for(int i=; i<n; i++)
{
if(s[i] == '(')
sta.push(s[i]);
else
{
if(!sta.empty())
sta.pop();
else
return ;
}
} if(!sta.empty())
return ;
return ;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n); scanf("%s", s); int Index1 = -, Index2 = -; for(int i=; i<n; i++)
{
if(Index1 == - && s[i] == ')')
Index1 = i;
if(s[i]=='(')
Index2 = i;
} swap(s[Index1], s[Index2]); if(Judge()) puts("Yes");
else puts("No");
}
return ;
}
Rikka with Parenthesis II---hdu5831(括号匹配)的更多相关文章
- 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 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(六花与括号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 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)
冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...
- HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场
题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...
- HDU 5831 Rikka with Parenthesis II
如果左括号数量和右括号数量不等,输出No 进行一次匹配,看匹配完之后栈中还有多少元素: 如果n=2,并且栈中无元素,说明是()的情况,输出No 如果n=2,并且栈中有元素,说明是)(的情况,输出Yes ...
随机推荐
- HBase--阿里未来发展
最近家里没网络,在公司加班写哈博客. HBase是一个开源的非关系型分布式数据库(NoSQL),基于谷歌的BigTable建模,是一个高可靠性.高性能.高伸缩的分布式存储系统,使用HBase技术可在廉 ...
- Django 数据传递
在前面的访问数据库中,我们是这样来插入数据的: [root@localhost web]$ cat web/urls.py urlpatterns = patterns('', .... url(r' ...
- C语言实现字符串IP与整数型IP的相互转换
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h&g ...
- 简单的Excel导入(上传、解析、持久化)
/** * excel导入 * @param req * @param resp * @return */ public void excelImport(){ //先将要上传的Excel文件上传到项 ...
- IOS设计模式第七篇之观察者设计模式
版权声明:原创作品,谢绝转载!否则将追究法律责任. 观察者设计模式 在观察者设计模式里面,一个对象通知其他的对象一些状态的改变.涉及这些对象不需要知道另一个对象---因此鼓励解耦设计模式.这个设计模式 ...
- 【面试题】新东方.NET工程师面试题总结
1.学校几本(是否统招).英语等级.大学成绩排名Top%几.当前月薪(入职前是否能提供薪资证明材料).期望月薪 二本,统招英语四级排名top10 2.做过的项目技术栈是什么?(例如 .NET.Sql ...
- linux 查看版本
输入"uname -a ",可显示电脑以及操作系统的相关信息. 输入"cat /proc/version",说明正在运行的内核版本.输入"cat /e ...
- Material Design系列第二篇——Getting Started
Getting Started This lesson teaches you to Apply the Material Theme Design Your Layouts Specify Elev ...
- JavaScript 正则表达式 通俗解释 快速记忆
1.正则表达式中最重要的三个符号: 1.1 B 在正则表达式中B有3种类型的括号: 1.1.1 方括号 “[“. 方括号"["内是需要匹配的字符.中括号括住的内容只匹配一个单一的字 ...
- Cordova 3.3 开发环境搭建(视频)
图文文章参见: http://www.cnblogs.com/mlzs/p/3332199.html 视频共享链接 百度:http://pan.baidu.com/s/1c0EHfqC