Submission #958515

#TimeUsernameProblemLanguageResultExecution timeMemory
958515MinaRagy06Palindromes (APIO14_palindrome)C++17
100 / 100
17 ms36836 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 300'005; struct node { int nxt[26]; int par, len, suflink, cnt; node() { memset(nxt, 0, sizeof nxt); par = len = suflink = cnt = 0; } } tree[N]; int nodes = 0, suf; void build() { nodes++; tree[nodes].len = -1; tree[nodes].suflink = nodes; nodes++; tree[nodes].len = 0; tree[nodes].suflink = nodes - 1; } ll ans = 0; int dfs(int i) { int cnt = tree[i].cnt; for (int j = 0; j < 26; j++) { if (!tree[i].nxt[j]) continue; cnt += dfs(tree[i].nxt[j]); } ans = max(ans, 1ll * cnt * tree[i].len); return cnt; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); string s; cin >> s; int n = s.size(); build(); suf = 1; for (int i = 0; i < n; i++) { int cur = suf; while (i - tree[cur].len - 1 < 0 || s[i - tree[cur].len - 1] != s[i]) { cur = tree[cur].suflink; } bool old = 1; if (!tree[cur].nxt[s[i] - 'a']) { old = 0; nodes++; tree[cur].nxt[s[i] - 'a'] = nodes; tree[nodes].len = tree[cur].len + 2; tree[nodes].par = cur; } int p = tree[cur].nxt[s[i] - 'a']; tree[p].cnt++; suf = p; if (old) continue; if (tree[p].len == 1) { tree[p].suflink = 2; continue; } cur = tree[cur].suflink; while (i - tree[cur].len - 1 < 0 || s[i - tree[cur].len - 1] != s[i]) { cur = tree[cur].suflink; } cur = tree[cur].nxt[s[i] - 'a']; tree[p].suflink = cur; } for (int i = nodes; i >= 1; i--) { // tree[tree[i].par].cnt += tree[i].cnt; tree[tree[i].suflink].cnt += tree[i].cnt; // cout << i << ": " << tree[i].len << ' ' << tree[i].cnt << ' ' << tree[i].par << ' ' << tree[i].suflink << '\n'; ans = max(ans, 1ll * tree[i].cnt * tree[i].len); } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...