Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
A rotation only changes where the string starts and ends. The order around the cycle stays the same.
Think of the string as a circle. A block boundary happens exactly between two neighboring characters that are different.
If the circular string has edges where adjacent characters differ, then after cutting one edge, the final line keeps all boundaries except possibly the cut one.
Cutting between equal characters is best: it keeps all different-adjacent boundaries inside the string, so the score becomes .
If there is no equal adjacent pair on the circle, every cut removes one boundary, so the answer is . Equivalently, compute circular differences and output .
Treat the string as circular first. Rotation does not reshuffle characters; it only chooses which circular edge gets cut to become the gap between the end and the beginning of the final string.
In any ordinary linear string, the number of blocks is
Every time two consecutive characters differ, a new block starts. Equal neighbors stay inside the same block. That's the whole game; no hidden dragon here.
Now put the string on a circle and count
These are the circular block boundaries. When we rotate the string, we choose one circular edge to cut. The final linear string contains the other edges as adjacent pairs.
There are two cases:
So we obviously prefer cutting between equal characters, if such an edge exists.
If at least one circular adjacent pair is equal, the answer is . If every circular adjacent pair is different, then , and every cut removes one boundary, so the score is . This is also captured by the compact formula
Why does this prove optimality? A rotation is exactly one cut in the circular string. For any chosen cut, the internal adjacent pairs are fixed: all circular edges except the cut edge. The score is always one plus the number of different internal edges. Therefore the only choice that matters is whether the cut edge itself was a boundary. Cutting a non-boundary is best when possible; otherwise all edges are boundaries and the score cannot exceed anyway.
Edge cases:
Complexity is per test case and extra memory besides the string.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
int diff = 0;
for (int i = 0; i < n; i++) {
if (s[i] != s[(i + 1) % n]) diff++;
}
cout << min(n, diff + 1) << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
int diff = 0;
for (int i = 0; i < n; i++) {
if (s[i] != s[(i + 1) % n]) diff++;
}
cout << min(n, diff + 1) << '\n';
}
}