CF2202B
Gaochenxi103_QAQ · · 题解
做法
一道简单好题。
首先考虑没有?如何判断,就用
而当其中有一个为负数时就说明非法。
那么加入?的考虑,就再维护一个 ?的数量。每次出现一个?时,如果
当 ?这么组合;到这个状态的时候相比于忽视?,只可能为交换当前
但要注意的是,如果当前有一个确定的位置且之前有奇数?的积累;那么在某种情况下这些问号的整体贡献就是确定的。
因为为奇数那么一定是增加一个减少一个,所以如果现在 ?组的贡献就一定是
代码
::::success[赛时代码]
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
string s;
void solve()
{
cin>>n>>s;s='#'+s;
int x,y,z=0;
if(n%2==0)y=1,x=1;
else y=0,x=2;
if(n==1 && s[1]!='b')
{
cout<<"Yes\n";
return ;
}
for(int i=1;i<=n;i++)
{
if(s[i]=='a')
{
if(z%2)
{
if(x==1)
{
x++,y--,z--;
}
}
x--,y++;
}
else if(s[i]=='b')
{
if(z%2)
{
if(y==1)
{
x--,y++,z--;
}
}
x++,y--;
}
else if(x==2)x--,y++;
else if(y==2)x++,y--;
else z++;
while(x<0 || y<0)
{
if(z>0)
{
if(z%2)
{
if(x<0)x++,y--;
else y++,x--;
z=0;
}
else
{
cout<<"No\n";
return ;
}
}
else
{
cout<<"No\n";
return ;
}
}
}
cout<<"Yes\n";
}
signed main()
{
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int T;
cin>>T;
while(T--)solve();
return 0;
}
/*
1
6
?abb??
*/
::::