fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int t;
  6. cin>>t;
  7. while(t--){
  8. int n;
  9. cin>>n;
  10. string s;
  11. cin>>s;
  12. int count=0,b=0;
  13. for(int i=0;i<n-1;i++){
  14. if(s[i]!=s[i+1]){
  15. count++;
  16. }
  17. else{
  18. b++;
  19. }
  20. }
  21. if(b>=1 && s[0]!=s[n-1]) cout<<count+2<<endl;
  22. else cout<<count+1<<endl;
  23. }
  24. return 0;
  25. }
Success #stdin #stdout 0s 5308KB
stdin
4
4
abcd
4
abbc
4
abba
6
abbccc
stdout
4
4
3
4