# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
921428 | AVIJIT_BISWAS | Palindromes (APIO14_palindrome) | C++14 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
using namespace std;
#define ll long long
struct PalindromicTree {
struct node {
int nxt[26], len, link, occ;
};
string s;
vector<node> t;
int sz, last;
PalindromicTree() {}
PalindromicTree(string _s) {
s = _s;
int n = s.size();
t.clear();
t.resize(n + 9);
sz = 2, last = 2;
t[1].len = -1, t[1].link = 1;
t[2].len = 0, t[2].link = 1;
}
int extend(int pos) { // returns 1 if it creates a new palindrome
int cur = last, curlen = 0;
int ch = s[pos] - 'a';
while (1) {
curlen = t[cur].len;
if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) break;
cur = t[cur].link;
}
if (t[cur].nxt[ch]) {
last = t[cur].nxt[ch];
return 0;
}
sz++;
last = sz;
t[sz].len = t[cur].len + 2;
t[cur].nxt[ch] = sz;
[last].occ = 1;
if (t[sz].len == 1) {
t[sz].link = 2;
return 1;
}
while (1) {
cur = t[cur].link;
curlen = t[cur].len;
if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) {
t[sz].link = t[cur].nxt[ch];
break;
}
}
return 1;
}
}T;
void solve() {
string s; cin >> s;
T = PalindromicTree(s);
int n = s.size();
for(int i = 0; i < n; i++){
T.extend(i);
}
ll ans = 0;
for(int i = T.sz; i > 2; i--) {
T.t[T.t[i].link].occ += T.t[i].occ;
ans = max(ans, 1ll * T.t[i].occ * T.t[i].len);
}
cout << ans << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++){
//cout << "-----Case:" << i << "-----\n";
solve();
}
return 0;
}