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.
Notice which character is completely impossible to change: the last one. No operation allows choosing .
Since never changes, the final string cannot be made of any character except the original last character.
So the real task is not “choose the best final character.” The final character is forced to be . Sneaky little constraint, but it carries the whole problem.
Any position with must be changed at least once. One operation only changes one position, so that already gives a lower bound.
That lower bound is achievable: for every position not equal to , change it after the nearest to its right has been propagated if needed. So the answer is just the count of characters not equal to the last character.
The operation is:
Choose some with , then set .
So information only moves left. A character can copy the character immediately to its right. Nothing ever copies left-to-right.
The key forced fact
The last character can never be changed, because the operation only lets us choose indices through .
That means the final string, if all characters are equal, must be:
There is no choice here. If you thought we were picking the most frequent character or doing some greedy “best target” thing, nope. The last character is the boss. Very democratic, obviously.
Lower bound
Look at any position where:
In the final string, this position must become , so it must be changed at least once.
Each operation changes exactly one position. Therefore, the answer is at least the number of characters not equal to .
Let that count be .
So:
Can we always do it in exactly that many operations?
Yes.
We can propagate copies of leftward. Start from the right side and move left. Whenever a character is not already equal to , change it by copying from the right after the right side has already been made good.
More intuitively: every bad character needs one bonk, and one bonk is enough. Characters that are already equal to need zero operations.
So:
Example
For abcabc, the last character is c.
Characters not equal to c are:
ababThere are , so the answer is .
Algorithm
For each test case:
target = s[n - 1].s are not equal to target.Complexity
For each test case, we scan the string once.
The constraints are tiny, so this is extremely chill.
#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;
char target = s.back();
int ans = 0;
for (char c : s) {
if (c != target) ans++;
}
cout << ans << '\n';
}
return 0;
}#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;
char target = s.back();
int ans = 0;
for (char c : s) {
if (c != target) ans++;
}
cout << ans << '\n';
}
return 0;
}