首页 技术 正文
技术 2022年11月12日
0 收藏 574 点赞 3,005 浏览 1936 个字

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Correct parentheses sequences can be defined recursively as follows: 
1.The empty string “” is a correct sequence. 
2.If “X” and “Y” are correct sequences, then “XY” (the concatenation of X and Y) is a correct sequence. 
3.If “X” is a correct sequence, then “(X)” is a correct sequence. 
Each correct parentheses sequence can be derived using the above rules. 
Examples of correct parentheses sequences include “”, “()”, “()()()”, “(()())”, and “(((())))”.

Now Yuta has a parentheses sequence SS, and he wants Rikka to choose two different position i,ji,j and swap Si,SjSi,Sj.

Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.

It is too difficult for Rikka. Can you help her?

InputThe first line contains a number t(1<=t<=1000), the number of the testcases. And there are no more then 10 testcases with n>100

For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.OutputFor each testcase, print “Yes” or “No” in a line.Sample Input

3
4
())(
4
()()
6
)))(((

Sample Output

Yes
Yes
No

Hint

For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.水题,注意两点1一定要交换所以"()"不行;2记录 l和r 遇到第一次r>l的情况和最后面的'('交换位置,之后就不能交换位置了
 #include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
#include<math.h>
using namespace std; int main()
{
int T,n;
int l,r,sum;
scanf("%d",&T);
char a[];
bool flag1,flag2;
while(T--)
{
scanf("%d",&n);
if(n==)
{
printf("Yes\n");
continue;
}
cin>>a;
l=;r=;sum=;
if(n==&&a[]=='('&&a[]==')')
printf("No\n");
else if(n%!=)
printf("No\n");
else
{
flag1=false;flag2=true;
for(int i=;i<n;i++)
{
if(a[i]=='(')
l++;
else if(a[i]==')')
r++;
if(r>l&&!flag1)
{
for(int j=n-;j>i;j--)
{
if(a[j]=='(')
{
a[i]='(';
a[j]=')';
l++;
r--;
flag1=true;
break;
}
}
}
else if(r>l&&flag1==true)
{
flag2=false;
break;
}
}
if(flag2&&l==r)
printf("Yes\n");
else
printf("No\n");
} }
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905