#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const ull P = 37;
ull pw[100001];
int n;
string s[2];
ull hsh[100001][2];
ll created[100001][26], destroyed[100001][2];
void calc_hashes() {
hsh[0][0] = hsh[0][1] = 1;
FOR(i, 0, n) FOR(j, 0, 2)
hsh[i + 1][j] = hsh[i][j] * P + (s[j][i] - 'a' + 1);
}
ull get_hash(int a, int b, bool reverse) {
return hsh[b + 1][reverse] - hsh[a][reverse] * pw[b - a + 1];
}
bool is_palindrome(int a, int b, int c, int d) {
return get_hash(a, b, false) == get_hash(n - d - 1, n - c - 1, true);
}
int main() {
cin >> s[0];
n = s[0].size();
s[1] = s[0];
reverse(s[1].begin(), s[1].end());
pw[0] = 1;
FOR(i, 0, n) pw[i + 1] = P * pw[i];
calc_hashes();
ll base = 0;
FOR(i, 0, n) FOR(j, i, i + 2) {
int l = 0, r = min(i + 1, n - j);
while (l != r) {
int mid = (l + r + 1) / 2;
if (is_palindrome(i - mid + 1, i, j, j + mid - 1)) l = mid;
else r = mid - 1;
}
int rad = r;
base += rad;
if (i == j) {
destroyed[j + 1][0]--, destroyed[j + rad][0]++;
destroyed[j + 1][1] += j + rad, destroyed[j + rad][1] -= j + rad;
destroyed[i - rad + 1][0]++, destroyed[i][0]--;
destroyed[i - rad + 1][1] -= i - rad, destroyed[i][1] += i - rad;
} else {
destroyed[j][0]--, destroyed[j + rad][0]++;
destroyed[j][1] += j + rad, destroyed[j + rad][1] -= j + rad;
destroyed[i - rad + 1][0]++, destroyed[i + 1][0]--;
destroyed[i - rad + 1][1] -= i - rad, destroyed[i + 1][1] += i - rad;
}
int x = i - rad, y = j + rad;
if (x < 0 || y >= n) continue;
rad++;
l = 0, r = min(i + 1, n - j) - rad;
while (l != r) {
int mid = (l + r + 1) / 2;
if (is_palindrome(x - mid, x - 1, y + 1, y + mid)) l = mid;
else r = mid - 1;
}
created[x][s[0][y] - 'a'] += r + 1;
created[y][s[0][x] - 'a'] += r + 1;
}
ll best = 0, x = 0, y = 0;
FOR(i, 0, n) {
x += destroyed[i][0], y += destroyed[i][1];
FOR(j, 0, 26) if (i != s[0][j] - 'a') {
best = max(best, created[i][j] - i * x - y);
}
}
cout << base + best;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1656 KB |
Output is correct |
2 |
Correct |
7 ms |
1532 KB |
Output is correct |
3 |
Correct |
7 ms |
1528 KB |
Output is correct |
4 |
Correct |
6 ms |
1144 KB |
Output is correct |
5 |
Correct |
7 ms |
1400 KB |
Output is correct |
6 |
Correct |
7 ms |
1528 KB |
Output is correct |
7 |
Correct |
7 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
24824 KB |
Output is correct |
2 |
Correct |
49 ms |
24824 KB |
Output is correct |
3 |
Correct |
50 ms |
24952 KB |
Output is correct |
4 |
Correct |
61 ms |
24800 KB |
Output is correct |
5 |
Correct |
65 ms |
24824 KB |
Output is correct |
6 |
Correct |
62 ms |
24824 KB |
Output is correct |
7 |
Correct |
62 ms |
24828 KB |
Output is correct |
8 |
Correct |
37 ms |
4472 KB |
Output is correct |
9 |
Correct |
64 ms |
24824 KB |
Output is correct |
10 |
Correct |
61 ms |
24824 KB |
Output is correct |
11 |
Correct |
47 ms |
24828 KB |
Output is correct |
12 |
Correct |
65 ms |
24824 KB |
Output is correct |
13 |
Correct |
71 ms |
24824 KB |
Output is correct |
14 |
Correct |
61 ms |
24824 KB |
Output is correct |
15 |
Correct |
63 ms |
24824 KB |
Output is correct |
16 |
Correct |
58 ms |
24824 KB |
Output is correct |
17 |
Correct |
60 ms |
24824 KB |
Output is correct |
18 |
Incorrect |
65 ms |
24824 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |