fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #pragma region Macros
  5. #define Faster ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  6. #define ll long long
  7. #define ld long double
  8. #define pii pair<int, int>
  9. #define pll pair<long long, long long>
  10. #define all(x) x.begin(), x.end()
  11. #define rall(x) x.rbegin(), x.rend()
  12. #define pb push_back
  13. #define ff first
  14. #define ss second
  15. #define endl '\n'
  16. #define yes cout << "YES" << endl
  17. #define no cout << "NO" << endl
  18. #define m1 cout << -1 << endl
  19. #pragma endregion
  20.  
  21. #pragma region Math
  22. const ll MOD = 1e9 + 7;
  23. ll gcd(ll a, ll b) { return __gcd(a, b); }
  24. ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
  25. #pragma endregion
  26.  
  27. void solve() {
  28. int n; cin>>n;
  29. string s; cin>>s;
  30. int ans=n-1, cnt=0;
  31.  
  32. for(int i=2;i<n;i++){
  33. if(s[i-2]==s[i]){
  34. if(cnt<2)
  35. cnt+=2;
  36. else cnt++;
  37. }
  38.  
  39. else {
  40. if(cnt>1)
  41. ans-=cnt-1;
  42. cnt=0;
  43. }
  44. }
  45. if(cnt>1) ans-=cnt-1;
  46. cout<<ans<<endl;
  47. }
  48.  
  49. int main() {
  50. Faster;
  51. int t = 1;
  52. cin >> t;
  53. while (t--) solve();
  54. return 0;
  55. }
Success #stdin #stdout 0s 5316KB
stdin
7
6
aaabcc
10
aaaaaaaaaa
6
abcdef
7
abacaba
6
cccfff
4
abba
5
ababa
stdout
4
1
5
3
3
3
1