hdu 5831 2016多校8
来源:互联网 发布:生化危机6优化怎么样 编辑:程序博客网 时间:2024/05/29 07:12
#include <iostream>#include <stdio.h>#include <vector>#include <algorithm>#include <string.h>using namespace std;#define N 100100char str[N];int main(){ int T; scanf(" %d",&T); while(T--){ int n; scanf(" %d",&n); //while(getchar()!='\n') continue; int l = 0; int c = 0; scanf(" %s",str); if(n==2&&strcmp(str,"()")==0){ puts("No"); continue; } for(int i=0;i<n;i++){ //char ch = getchar(); char ch = str[i]; if(ch==')'){ l--; }else{ l++; } if(l==-1){ l=0; c++; } } //printf("%d %d\n",l,c); if(c>1||(l-c)!=0){ puts("No"); }else{ puts("Yes"); } //fflush(stdout); } return 0;}
题意:给一个长度为100000的括号组成的串,是否可以通过交换两个字符使得他匹配。
题解:简单题,但是有坑。之所以记下来是因为自己被坑了。
using namespace std;#define N 100100char str[N];bool check(int n){ int l = 0; for(int i=0;i<n;i++){ if(str[i]=='('){ l++; }else{ l--; } if(l<0) return false; } return l==0;}int main(){ int T; scanf(" %d",&T); while(T--){ int n; scanf(" %d",&n); scanf(" %s",str); if(n==2&&strcmp(str,"()")==0){ puts("No"); continue; } if(check(n)){ puts("Yes"); continue; } for(int i=0;i<n;i++){ if(str[i]==')'){ for(int j=n-1;j>=0;j--){ if(str[j]=='('){ swap(str[i],str[j]); break; } } break; } } if(check(n)){ puts("Yes"); }else{ puts("No"); } //fflush(stdout); } return 0;}
0 0
- hdu 5831 2016多校8
- hdu 5821 2016多校8
- 2016多校8 HDU 5821 Ball
- 2016 多校 hdu 5723
- 2016多校&&HDU.5738
- 2016多校 1011 hdu tetrahedron
- hdu 5739 2016多校2
- HDU 5739 Fantasia 2016多校
- hdu 5787 2016多校5
- hdu 5792 2016多校5
- hdu 5783 2016多校5
- hdu 5791 2016多校5
- hdu 5781 2016多校5
- hdu 5795 2016多校6
- hdu 5800 2016多校6
- hdu 5798 2016多校6
- hdu 5831 Rikka with Parenthesis II 2016 Multi-University 8
- HDU 5831
- POJ - 1759 Garland(二分)
- 关于注销账号
- iOS -- 生成一张Image
- 概率dp HDU4336 Card Collector
- Service 启动和绑定流程
- hdu 5831 2016多校8
- Spring jdbc 需要的包
- HDU 1198 Farm Irrigation 并查集
- 【JZOJ 4678】钱仓
- HDU 1051 Wooden Sticks【LIS变形】
- HDOJ 1257 最少拦截系统
- Oracle 12c新特性--数据编写(Redaction)
- fleury算法求欧拉路径(欧拉回路)模板
- C#iTextSharp字符串是否在pdf中