hdu_5831_Rikka with Parenthesis II(模拟)

时间:2023-03-09 20:05:10
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 ;
}