Codeforces Round #789 div2 B Tokitsukaze and Good 01-String

贪心,将字符串分成多个二元子串,如果是01或者10就需要操作一次,因为连续的11或者00串不会改变,所以最小子串数即为不需要改变的11或者00串的数量
特殊情况:只有01或者10时,数量为1
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
int tt;
for(cin>>tt;tt;tt--){
int n;
string s;
cin>>n>>s;
int cnt=0,tot=0,p=-1;
for(int i=0;i<n;i+=2){
if(s[i]!=s[i+1]){
cnt++;
}
else{
if(s[i]!=p) tot++;
p=s[i];
}
}
cout<<cnt<<" "<<max(1,tot)<<endl;
}
return 0;
}

浙公网安备 33010602011771号