-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathD - String Deletion.cpp
144 lines (116 loc) · 2.71 KB
/
D - String Deletion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n"
#define MAX 1000000
#define mod 1000000007
#define ll long long int
#define ld long double
#define ull unsigned long long
#define li long int
#define str string
#define fr(i,n) for(ll i = 0; i<n; i++)
#define frj(j,i,n) for(ll j = i; j<n; j++)
#define frev(i,n) for(ll i = n-1; i>=0; i--)
#define all(x) x.begin(),x.end()
#define debug(x) cout << #x << " " << x << endl;
#define println(x) cout << x << "\n";
#define print(x) cout << x << " ";
#define printCase(x) cout << "Case #" << x << ": ";
#define umax(a, b) a=max(a,b);
template<class A> void read(vector<A>& v);
template<class T> void read(T& x) {
cin >> x;
}
void read(double& d) {
string t;
read(t);
d=stod(t);
}
void read(long double& d) {
string t;
read(t);
d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
read(h);
read(t...);
}
template<class A> void write(A x) {
cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
write(h);
write(t...);
}
void solve() {
ll n;
read(n);
string s;
read(s);
int c = 1, i = 1;
ll ans = 0;
vector<int> count;
while (i < n){
if (s[i] == s[i-1]) {
c++;
}
else {
count.push_back(c);
c = 1;
}
++i;
}
count.push_back(c);
i = 0;
int pos = -1;
fr (i, count.size())
if (count[i] > 1){
pos = i;
break;
}
if (pos == -1) {
cout << ceil((float)n/2) << endl;
// debug("yes\n+");
return;
}
ans = 0;
// debug(pos);
fr (j, count.size()) {
count[pos]--;
ans++;
if (pos == j || count[pos] == 1) {
while (pos < count.size()) {
pos++;
if (count[pos] > 1)
break;
}
if(pos >= count.size()) {
ans += ceil((float)(count.size()-j-1)/2);
break;
}
}
}
cout << ans << endl;
// fr (i, count.size())
// cout << count[i] << " ";
// cout << endl;
}
int main() {
fastio;
//Skipped in presense of online judge.
#ifndef ONLINE_JUDGE
freopen("D:/Competitive/inputf.in","r",stdin);
freopen("D:/Competitive/outputf.in","w",stdout);
#endif
int t(1);
cin >> t;
frj(i, 1, t+1) {
// printCase(i);
solve();
}
return 0;
}