Untitled
unknown
c_cpp
4 years ago
1.8 kB
7
Indexable
//seg tree without lazy propagation
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, x, y) for (int i = (x); i <= (y); i++)
#define forr(i, x, y) for (int i = (x); i < (y); i++)
#define forrl(i, x, y) for (ll i = (x); i < (y); i++)
#define dow(i, x, y) for (int i = (x); i >= (y); i--)
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fil(a, b) memset((a), (b), sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
const int MOD=1e9+7;
const double pi=acos(-1);
const int MAX=2*1e5+5;
const int N=1e6+5;
struct ff{
int a;
int b;
int c;
int least_b;
int least_c;
};
string s;
ll ans;
ff seg_Tree[4*N];
ll n,x,y;
bool found;
ff merg(ff l,ff r){
return {l.a+r.a,l.b+r.b,l.c+r.c,};
}
void build(int id=0,int ns=0,int ne=n-1){
if(found ){
return;
}
if(ns==ne){
seg_Tree[id]={s[ns]=='a',s[ns]=='b',s[ns]=='c'};
return;
}
int left=2*id+1;//left id of seg tree
int right=left+1;//right id
int mid= (ns+ne)/2;//mid range
build(left,ns,mid);
build(right,mid+1,ne);
seg_Tree[id]=merg(seg_Tree[left],seg_Tree[right]);
if(!found&&ne-ns+1>=2&&
seg_Tree[id].a>seg_Tree[id].b&&
seg_Tree[id].a>seg_Tree[id].c){
found=true;
ans=ne-ns+1;
}
}
void solve(){
cin>>n>>s;
found =false;
build();
if(found){
cout<<ans<<endl;
}else{
cout<<-1<<endl;
}
}
int main(){
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
ll t=1;
//init();
cin>>t;
for (ll i = 1; i <= t; i++)
{
// cout<<"Case "<<i<<": ";
solve();
}
return 0;
} Editor is loading...