#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
string S; int L[100009], N, col[100009], cnts; vector<int>x[100009], y[100009];
long long Get(int pos) {
if (pos < 0) return -1;
if (pos >= N) return -2;
return L[pos];
}
long long roll1[1009][1009], roll2[1009][1009];
void solve_naive() {
map<long long, long long>M;
for (int i = 0; i < S.size(); i++) {
for (int j = i; j < S.size(); j++) {
if (j != 0) roll1[i][j] = roll1[i][j - 1] * 331LL;
roll1[i][j] += (S[j] - 'a' + 1);
}
}
for (int i = 0; i < S.size(); i++) {
for (int j = i; j >= 0; j--) {
roll2[i][j] = roll2[i][j + 1] * 331LL;
roll2[i][j] += (S[j] - 'a' + 1);
}
}
long long maxn = 0;
for (int i = 0; i < S.size(); i++) {
for (int j = i; j < S.size(); j++) {
if (roll1[i][j] != roll2[j][i]) continue;
M[roll1[i][j]] += (j - i + 1);
maxn = max(maxn, M[roll1[i][j]]);
}
}
cout << maxn << endl;
}
int main() {
cin >> S; N = S.size();
if (S.size() == 1) { cout << "1" << endl; return 0; }
if (S.size() <= 1000) { solve_naive(); return 0; }
for (int i = 0; i < N; i++) L[i] = (S[i] - 'a');
for (int i = 0; i < N; i++) x[0].push_back(i);
long long maxn = 0; cnts = 1;
for (int i = 0; i <= N / 2 + 1; i++) {
int cnt = 0;
for (int j = 0; j < cnts; j++) {
if (x[j].size() == 1) { int to = x[j][0]; if (Get(to - i) == Get(to + i)) { y[cnt] = x[j]; cnt++; } continue; }
vector<int>H[26];
for (int k = 0; k < x[j].size(); k++) {
int to = x[j][k];
if (Get(to - i) != Get(to + i)) continue;
H[Get(to + i)].emplace_back(to);
}
for (int k = 0; k < 26; k++) {
if (H[k].size() >= 1) { y[cnt] = H[k]; cnt++; }
}
}
for (int j = 0; j < cnt; j++) { x[j] = y[j]; maxn = max(maxn, 1LL * (long long)x[j].size()*(i * 2 + 1)); } cnts = cnt;
}
for (int i = 0; i < 100009; i++) x[i].clear(); cnts = 1;
for (int i = 0; i < N; i++) x[0].push_back(i);
for (int i = 0; i <= N / 2 + 1; i++) {
int cnt = 0;
for (int j = 0; j < cnts; j++) {
if (x[j].size() == 1) { int to = x[j][0]; if (Get(to - i) == Get(to + i)) { y[cnt] = x[j]; cnt++; } continue; }
vector<int>H[26];
for (int k = 0; k < x[j].size(); k++) {
int to = x[j][k];
if (Get(to - i) != Get(to + i + 1)) continue;
H[Get(to + i)].emplace_back(to);
}
for (int k = 0; k < 26; k++) {
if (H[k].size() >= 1) { y[cnt] = H[k]; cnt++; }
}
}
for (int j = 0; j < cnt; j++) { x[j] = y[j]; maxn = max(maxn, 1LL * (long long)x[j].size()*(i * 2 + 2)); }
cnts = cnt;
}
cout << maxn << endl;
return 0;
}
Compilation message
palindrome.cpp: In function 'void solve_naive()':
palindrome.cpp:21:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < S.size(); i++) {
~~^~~~~~~~~~
palindrome.cpp:22:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = i; j < S.size(); j++) {
~~^~~~~~~~~~
palindrome.cpp:27:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < S.size(); i++) {
~~^~~~~~~~~~
palindrome.cpp:34:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < S.size(); i++) {
~~^~~~~~~~~~
palindrome.cpp:35:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = i; j < S.size(); j++) {
~~^~~~~~~~~~
palindrome.cpp: In function 'int main()':
palindrome.cpp:59:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < x[j].size(); k++) {
~~^~~~~~~~~~~~~
palindrome.cpp:71:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for (int i = 0; i < 100009; i++) x[i].clear(); cnts = 1;
^~~
palindrome.cpp:71:49: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for (int i = 0; i < 100009; i++) x[i].clear(); cnts = 1;
^~~~
palindrome.cpp:80:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < x[j].size(); k++) {
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
5 ms |
5220 KB |
Output is correct |
3 |
Correct |
6 ms |
5220 KB |
Output is correct |
4 |
Correct |
7 ms |
5220 KB |
Output is correct |
5 |
Correct |
5 ms |
5220 KB |
Output is correct |
6 |
Correct |
5 ms |
5220 KB |
Output is correct |
7 |
Correct |
6 ms |
5220 KB |
Output is correct |
8 |
Correct |
5 ms |
5220 KB |
Output is correct |
9 |
Correct |
6 ms |
5248 KB |
Output is correct |
10 |
Correct |
6 ms |
5384 KB |
Output is correct |
11 |
Correct |
6 ms |
5560 KB |
Output is correct |
12 |
Correct |
5 ms |
5560 KB |
Output is correct |
13 |
Correct |
6 ms |
5560 KB |
Output is correct |
14 |
Correct |
6 ms |
5584 KB |
Output is correct |
15 |
Correct |
6 ms |
5584 KB |
Output is correct |
16 |
Correct |
6 ms |
5584 KB |
Output is correct |
17 |
Correct |
6 ms |
5584 KB |
Output is correct |
18 |
Correct |
5 ms |
5584 KB |
Output is correct |
19 |
Correct |
6 ms |
6092 KB |
Output is correct |
20 |
Correct |
7 ms |
6100 KB |
Output is correct |
21 |
Correct |
7 ms |
6228 KB |
Output is correct |
22 |
Correct |
6 ms |
6228 KB |
Output is correct |
23 |
Correct |
7 ms |
6228 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
25 |
Correct |
7 ms |
6228 KB |
Output is correct |
26 |
Correct |
6 ms |
6228 KB |
Output is correct |
27 |
Correct |
6 ms |
6228 KB |
Output is correct |
28 |
Correct |
6 ms |
6228 KB |
Output is correct |
29 |
Correct |
6 ms |
6228 KB |
Output is correct |
30 |
Correct |
6 ms |
6240 KB |
Output is correct |
31 |
Correct |
6 ms |
6240 KB |
Output is correct |
32 |
Correct |
6 ms |
6240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
19044 KB |
Output is correct |
2 |
Correct |
36 ms |
19044 KB |
Output is correct |
3 |
Correct |
87 ms |
19204 KB |
Output is correct |
4 |
Correct |
27 ms |
19204 KB |
Output is correct |
5 |
Correct |
90 ms |
19204 KB |
Output is correct |
6 |
Correct |
86 ms |
19204 KB |
Output is correct |
7 |
Correct |
25 ms |
19204 KB |
Output is correct |
8 |
Correct |
57 ms |
19204 KB |
Output is correct |
9 |
Correct |
26 ms |
19204 KB |
Output is correct |
10 |
Correct |
24 ms |
19204 KB |
Output is correct |
11 |
Correct |
24 ms |
19204 KB |
Output is correct |
12 |
Correct |
25 ms |
19204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
19204 KB |
Output is correct |
2 |
Correct |
114 ms |
19204 KB |
Output is correct |
3 |
Correct |
370 ms |
19204 KB |
Output is correct |
4 |
Correct |
275 ms |
19204 KB |
Output is correct |
5 |
Correct |
8 ms |
19204 KB |
Output is correct |
6 |
Correct |
18 ms |
19204 KB |
Output is correct |
7 |
Correct |
52 ms |
19204 KB |
Output is correct |
8 |
Correct |
6 ms |
19204 KB |
Output is correct |
9 |
Correct |
6 ms |
19204 KB |
Output is correct |
10 |
Correct |
7 ms |
19204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1082 ms |
19204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
22 ms |
19204 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |