It’s from a recent contest which has finished !!
the problem link is :- Problem - B - Codeforces
my code :-
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
for ( int k =0; k<T; k++){
int n ;
cin>>n;
set <int> m ;
set<int>::iterator itr1, itr2;
char arr[n] ;
int countT = 0;
int countM = 0;
int req = (n/3)*2 ;
int t[req] ; // array t stores indexes where char 'T' is present in char arr array
int j = 0;
for (int i =0; i<n; i++){
cin>>arr[i] ;
if ( arr[i] == 'T'){
t[j] = i ;
j++ ;
countT++ ;
}
else {
countM++ ;
m.insert( i );
}
}
bool set = true ;
if ( countT != (n / 3)*2 || countM != (n/3) )
set = false ;
else if ( arr[0] != 'T' || arr[n-1] != 'T')
set = false ;
else {
int turns = n/3 ;
int itr = 0 ;
while ( itr < turns ){
int v1 = t[itr] ;
int v2 = t[itr + turns] ;
bool test = m.upper_bound(v1) != m.lower_bound(v2);
if ( test == 1){
auto itm = m.lower_bound( v1) ;
m.erase(itm) ;
}
else{
set = false ;
break ;
}
++itr ;
}
}
if ( set == false )
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
return 0;
}
on code forces diagnostics I got this error :-
Diagnostics detected issues [cpp.clang+±diagnose]: p71.cpp:51:17: runtime error: index 24 out of bounds for type ‘int [req]’
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior p71.cpp:51:17 in
And when run on codechef ide without any custom input, it sometimes gives SIGTSTP runtime error .