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.
Start by ignoring long strings. How many times can a hidden string of length occur? What about length ? For length , notice that any pair of indices forms an arithmetic progression.
Now look at a hidden string of length at least . If you know the first two chosen indices , what is the common difference ? And once is fixed, how many choices are left for the remaining indices?
That means every occurrence of a string with is uniquely determined by an occurrence of just its prefix . So the number of occurrences of any longer string is at most the number of occurrences of some length- string.
So the answer is simply
where is the number of pairs with , , .
Count in one left-to-right pass. Maintain how many times letter has appeared so far. When you read current letter , every previous creates a new subsequence , so do
Then increment . Use long long, because the answer can be about .
The whole trick is realizing that long hidden strings are mostly bluffing.
Let be the given string.
Why is length this easy? Because any two indices form an arithmetic progression. No extra restriction. Zero drama.
So if we define:
and
then every hidden string of length or is handled by these values.
Now take some hidden string
An occurrence of uses indices
that form an arithmetic progression. So there exists a common difference such that
The moment you know and , you know
and therefore all remaining indices are forced:
So for a fixed string , each occurrence is uniquely determined by the first two chosen positions.
That gives an injection:
Therefore,
This is the key punchline:
No hidden string of length at least can occur more times than some hidden string of length .
So the answer is simply
That’s the entire problem. Yep, really.
We need to compute efficiently.
Process the string from left to right. Suppose the current character is .
For every letter :
If cnt[a] stores how many times letter has already appeared, then when reading character :
Then we do
Since there are only lowercase letters, this is
which is basically linear.
We’ve already shown two facts:
So taking the maximum over all single letters and all ordered pairs covers all possible hidden strings.
No fancy DP table over positions, no arithmetic progression enumeration, no nonsense. Just count letters and ordered pairs.
Use long long, because the number of pairs can be as large as
That does not fit in int. Don’t throw the submission away over something that dumb.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
string s;
cin >> s;
ll cnt[26] = {};
ll pairs[26][26] = {};
ll ans = 0;
for (char ch : s) {
int c = ch - 'a';
for (int a = 0; a < 26; ++a) {
pairs[a][c] += cnt[a];
}
cnt[c]++;
ans = max(ans, cnt[c]);
}
for (int a = 0; a < 26; ++a) {
for (int b = 0; b < 26; ++b) {
ans = max(ans, pairs[a][b]);
}
}
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();
string s;
cin >> s;
ll cnt[26] = {};
ll pairs[26][26] = {};
ll ans = 0;
for (char ch : s) {
int c = ch - 'a';
for (int a = 0; a < 26; ++a) {
pairs[a][c] += cnt[a];
}
cnt[c]++;
ans = max(ans, cnt[c]);
}
for (int a = 0; a < 26; ++a) {
for (int b = 0; b < 26; ++b) {
ans = max(ans, pairs[a][b]);
}
}
cout << ans << '\n';
return 0;
}