显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
string s;
int ans;
int main(){
freopen("moocaozuo.in","r",stdin);
freopen("moocaozuo.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin>>q;
for(int j=1;j<=q;j++){
cin>>s;
if(s.length()<2){
cout<<"-1"<<endl;
continue;
}
ans=0x3f3f3f3f;
for(int i=0;i<s.length()-2;i++){
if(s[i]=='M'){
if(s[i+1]=='O'){
if(s[i+2]=='O'){
ans=0;
}
else{
ans=min(ans,1);
}
}
}
else{
if(s[i+1]=='O'){
if(s[i+2]=='O'){
ans=min(ans,1);
}
else{
ans=min(ans,2);
}
}
}
}
if(ans==0x3f3f3f3f){
cout<<"-1"<<endl;
}
else{
cout<<ans+s.length()-3<<endl;
}
}
return 0;
}