算法:双指针
思路: 我们要让操作次数最大化 用贪心的思路想 我们每次操作分两段 第一段随便删除一个 第二段删除最长相同前缀 我们能控制的为第一段的删除 因为每次会删除一个相同的前缀子串 所以我们每次删除不应该减少相同子串的数量 从最前面考虑 因为它会更早的被第二段的操作删除 每次第一段操作删除最靠前的有相同的子串的一个 这个我们可以用一个双指针解决 先开两个指针l和r l代表我当前第二段删除到哪里了 每次删除 让l移动到一个子串的最后一个节点 然后l++到下一个子串 r代表当前第一段删除到哪了 r去寻找从l开始最近的相同子串 找到并删除一个r++ 如果r到n个之后了 代表我当前没有任何一个相同子串了 形如(10101010)这种 那我只能删除最靠前的一个 让l++ 每次操作使ans++
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;using ll = long long;using PLL = pair<ll,ll>;
const ll MAX = 1e18;const ll MIN = -1e18;const ll INF=0x3f3f3f3f;
const ll Q = 2e5+9;const ll MOD = 1e9 + 7;
string s;
void solve(){
ll n;cin>>n;
cin>>s;
ll ans=0;
s=" "+s;
ll l=1,r=1;
while(l<=n){
if(r<=l) r=l;
while(s[r]!=s[r+1] and r+1<=n){
r++;
}r++;
while(s[l]==s[l+1] and l+1<=n){
l++;
}l++;
if(r>n) {
while(s[l]==s[l+1] and l+1<=n){
l++;
}l++;
}
ans++;
}
cout<<ans<<"\n";
}
int main(){
ios;ll _=1;cin>>_;
while (_--)solve();
return 0;
}