제출 #400303

#제출 시각아이디문제언어결과실행 시간메모리
400303BERNARB01회문 (APIO14_palindrome)C++17
100 / 100
37 ms39892 KiB
#include <bits/stdc++.h> using namespace std; template<const int A> class ertre { string s; int n, I, id, cr; vector<int> L, P, D, C; vector<array<int, A>> T; int pr(int v) { while (I - L[v] - 2 < 0 || s[I - L[v] - 2] != s[I - 1]) { v = P[v]; } return v; } void add() { int ch = s[I++] - 'a'; cr = pr(cr); if (!T[cr][ch]) { L[id] = 2 + L[cr]; P[id] = T[pr(P[cr])][ch]; D[id] = 1 + D[P[id]]; T[cr][ch] = id++; } cr = T[cr][ch]; C[cr]++; } public: ertre() {}; ertre(const string& _s) { s = _s; n = s.length(); I = cr = 0; id = 2; L.assign(n + 2, 0); P.assign(n + 2, 0); C.assign(n + 2, 0); D.assign(n + 2, 0); T.assign(n + 2, {}); P[0] = 1; L[1] = -1; for (int i = 0; i < n; i++) { add(); } } long long solve() { vector<int> inDeg(n + 2, 0); for (int i = 2; i < n + 2; i++) { inDeg[P[i]]++; } vector<int> que; for (int i = 2; i < n + 2; i++) { if (inDeg[i] == 0) { que.push_back(i); } } for (int b = 0; b < (int) que.size(); b++) { int u = que[b]; int v = P[u]; if (v < 2) { continue; } C[v] += C[u]; inDeg[v]--; if (inDeg[v] == 0) { que.push_back(v); } } long long res = 0; for (int i = 2; i < n + 2; i++) { res = max(res, C[i] * 1LL * L[i]); } return res; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; cout << ertre<26>(s).solve() << '\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...