Submission #493247

#TimeUsernameProblemLanguageResultExecution timeMemory
493247MladenPPalindromes (APIO14_palindrome)C++17
100 / 100
75 ms63236 KiB
#include <bits/stdc++.h> #define PRINT(x) cerr<<#x<<'='<<x<<endl; using namespace std; struct PalTree { #define P_SIGMA 26 #define FIRST_NODE 1 #define SECOND_NODE 2 struct Node { int len, cnt, dub, slink; int to[P_SIGMA]; Node() { len = cnt = dub = slink = 0; for(int i = 0; i < P_SIGMA; i++) to[i] = 0; } Node(int _len, int _cnt, int _dub, int _slink) { len = _len; cnt = _cnt; dub = _dub; slink = _slink; for(int i = 0; i < P_SIGMA; i++) to[i] = 0; } }; string s; vector<Node> pt; int suff; int toidx(char c) { return c-'a'; } void addLetter(char c) { s += c; int len = s.size(); while(c != s[len-2-pt[suff].len]) { suff = pt[suff].slink; } int nd = pt[suff].to[toidx(c)]; if(nd == 0) { nd = pt.size(); pt[suff].to[toidx(c)] = nd; pt.push_back(Node(pt[suff].len+2, 0, 0, 0)); if(suff == FIRST_NODE) { pt[nd].slink = SECOND_NODE; } else { int u = pt[suff].slink; while(c != s[len-2-pt[u].len]) { u = pt[u].slink; } pt[nd].slink = pt[u].to[toidx(c)]; } pt[nd].dub = pt[pt[nd].slink].dub+1; } pt[nd].cnt++; suff = nd; } void init() { pt.clear(); s = ""; suff = 2; pt.push_back(Node(0, 0, 0, 0)); pt.push_back(Node(-1, 0, 0, FIRST_NODE)); ///first node pt.push_back(Node(0, 0, 0, FIRST_NODE)); ///second node } Node &operator[](int idx) { if(idx >= pt.size()) { cerr << "Index out of bounds" <<endl; return pt[0]; } return pt[idx]; } }; PalTree pt; int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; pt.init(); for(auto x : s) { pt.addLetter(x); } long long rez = 0; for(int i = pt.pt.size()-1; i > 2; i--) { rez = max(rez, (long long) pt[i].len*pt[i].cnt); pt[pt[i].slink].cnt += pt[i].cnt; } cout << rez << endl; return 0; }

Compilation message (stderr)

palindrome.cpp: In member function 'PalTree::Node& PalTree::operator[](int)':
palindrome.cpp:55:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<PalTree::Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |         if(idx >= pt.size()) {
      |            ~~~~^~~~~~~~~~~~
#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...