Submission #703247

#TimeUsernameProblemLanguageResultExecution timeMemory
703247600MihneaPalindromes (APIO14_palindrome)C++17
0 / 100
41 ms41892 KiB
#include <cmath> #include <functional> #include <fstream> #include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <map> #include <list> #include <time.h> #include <math.h> #include <random> #include <deque> #include <queue> #include <unordered_map> #include <unordered_set> #include <iomanip> #include <cassert> #include <bitset> #include <sstream> #include <chrono> #include <cstring> #include <numeric> using namespace std; #define int long long int dumb(string s) { int n = (int)s.size(); vector<vector<int>> lcp(n, vector<int>(n, 0)); vector<vector<bool>> ispal(n, vector<bool>(n, 0)); for (int i = 0; i < n; i++) { lcp[i][i] = n - i; } for (int i = n - 1; i >= 0; i--) { for (int j = i + 1; j < n; j++) { if (s[i] == s[j]) { int c = 0; if (i + 1 < n && j + 1 < n) { c += lcp[i + 1][j + 1]; } lcp[i][j] = lcp[j][i] = 1 + c; } } } for (int i = 0; i < n; i++) { ispal[i][i] = 1; } for (int i = 0; i + 1 < n; i++) { ispal[i][i + 1] = (s[i] == s[i + 1]); } for (int len = 3; len <= n; len++) { for (int i = 0; i + len - 1 < n; i++) { int j = i + len - 1; ispal[i][j] = ispal[i + 1][j - 1] & (s[i] == s[j]); } } int sol = 0; for (int len = 1; len <= n; len++) { for (int i = 0; i + len - 1 < n; i++) { int j = i + len - 1; if (ispal[i][j]) { int matches = 0; for (int k = 0; k < n; k++) { matches += (lcp[k][i] >= len); } sol = max(sol, len * matches); } } } return sol; } struct Vertex { int fail; int len; vector<int> kids; int cnt; }; vector<Vertex> verts; vector<int> mt(26, 0); int smart(string s) { verts.clear(); verts.push_back({ 0, -1, mt, 0 }); verts.push_back({ 0, 0, mt, 0 }); assert((int)s.size() >= 1); s = "$" + s; assert((int)s.size() >= 2); int n = (int)s.size(); int curvert = 0; for (int i = 1; i < n; i++) { while (s[i] != s[i - verts[curvert].len - 1]) { assert(curvert >= 1); curvert = verts[curvert].fail; } assert(s[i] == s[i - verts[curvert].len - 1]); { int x = s[i] - 'a'; assert(0 <= x && x < 26); if (!verts[curvert].kids[x]) { int fail = verts[curvert].fail; assert(fail >= 0); while (s[i] != s[i - verts[fail].len - 1]) { assert(fail >= 1); fail = verts[fail].fail; } if (fail < 1) { fail = 1; } else { assert(verts[fail].kids[x]); fail = verts[fail].kids[x]; } verts.push_back({ fail, verts[curvert].len + 2, mt, 0 }); verts[curvert].kids[x] = (int)verts.size() - 1; } curvert = verts[curvert].kids[x]; } verts[curvert].cnt++; } vector<int> q = { 0, 1 }; for (int i = 0; i < (int)q.size(); i++) { int a = q[i]; for (int x = 0; x < 26; x++) { if (verts[a].kids[x]) { q.push_back(verts[a].kids[x]); } } }/* for (auto& x : q) { cout << x << " "; } cout << "\n";*/ int sol = 0; for (int _ = (int)q.size() - 1; _ >= 0; _--) { int who = q[_]; verts[verts[who].fail].cnt += verts[who].cnt; sol = max(sol, verts[who].len * verts[who].cnt); } return sol; exit(0); } signed main() { #ifdef ONPC FILE* stream; freopen_s(&stream, "input.txt", "r", stdin); #else ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif string s; cin >> s; //cout << dumb(s) << "\n"; cout << smart(s) << "\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...