题解:P14172 【MX-X23-T2】括号串
这是一道结论题
首先,一个括号串想要合法,对于每一对括号,一定是左括号在前,右括号在后(而且长度必为偶数),所以,我们不难得出结论,对于任意的
那么特殊情况怎么处理?题目说能且只能改一对 ")(" ,那么,我们不难发现,当首次出现 ")("时,即遍历到 ")" 时,"(" ,使
以下为我的
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
for(int u=0;u<T;u++)
{
int n;
string s;
cin>>n>>s;
if(s.length()%2)
{
cout<<"No"<<endl;
continue;
}
int cnt=0;
int c=0;
for(int x=0;x<n;x++)
{
if(s[x]=='(')
{
cnt++;
}
else
{
cnt--;
}
if(cnt<0)
{
if(c)
{
break;
}
c++;
}
}
if(cnt==0&&c<2)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
}