链接:https://vjudge.net/contest/400848#problem/D
注:a/2向上取整就是(a+1)/2
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
char s[maxn];
int a[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
scanf("%s", s+1);
int now = 1, tot = 0;
for(int i = 2; i <= n; i++) {
if(s[i] == s[i-1]) {
now++;
}
else{
a[++tot] = now;
now = 1;
}
}
a[++tot] = now;
int ans = 0;
for(int i = 1; i <= tot; i++) {
if(a[i] > 1) {
ans++;
}
else {
bool flag = 0;
for(int j = i + 1; j <= tot; j++) {
if(a[j] > 1) {
a[j]--;
ans++;
flag = 1;
break;
}
}
if(!flag) {
ans += (tot-i+2)/2;
break;
}
}
}
cout << ans << endl;
}
return 0;
}
京公网安备 11010502036488号