This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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++) {
~~^~~~~~~~~~~~~
# | 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... |