이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const p = 31;
int const mod = 1e9 + 9;
int const maxn = 10010;
char s[maxn];
int h[maxn];
int pw[maxn];
vector<int> hs(int n) {
vector<int> res(n+1);
for (int i = 1; i <= n; i++) {
res[i] = (ll(res[i-1])*p + s[i] ) % mod;
}
return res;
}
void init() {
pw[0] = 1;
for (int i = 1; i < maxn; i++)
pw[i] = ll(pw[i-1])*p%mod;
}
int main() {
init();
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> s[i];
auto fh = hs(n);
reverse(s+1, s+n+1);
auto rh = hs(n);
// ll ans = 0;
auto comp = [&] (int l, int r) -> pair<int, int> {
int x = ((ll(fh[r]) - ll(fh[l-1])*pw[r-l+1]) % mod + mod) % mod;
int y = ((ll(rh[n-l+1]) - ll(rh[n-r])*pw[r-l+1]) % mod + mod) % mod;
return {x == y, x};
};
ll ans = 0;
for (int lenght = 1; lenght <= n; lenght++) {
vector<int> v;
for (int l = 1; l + lenght - 1 <= n; l++) {
int r = l + lenght - 1;
auto x = comp(l, r);
if (x.first) {
v.push_back(x.second);
}
}
sort(v.begin(), v.end());
int best = 0;
for (int i = 0; i < (int)v.size(); i++) {
int l = i;
while (i < (int)v.size() and (i == 0 or v[i] == v[i-1])) i++;
best = max(best, i - l);
}
ans = max(ans, ll(lenght)*best);
// cout << lenght << " " << best << "\n";
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |