Submission #547699

#TimeUsernameProblemLanguageResultExecution timeMemory
547699MilosMilutinovicPalindromes (APIO14_palindrome)C++14
100 / 100
53 ms63268 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 300000;

/* template by MladenP */
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 pp;

int main() {
	char ss[N];
	int n, i;

	scanf("%s", ss);
	pp.init(), n = strlen(ss);
	for (i = 0; i < n; i++)
		pp.addLetter(ss[i]);
	long long ans = 0;
	for (i = pp.pt.size() - 1; i > 2; i--) {
		ans = max(ans, (long long) pp[i].len * pp[i].cnt);
		pp[pp[i].slink].cnt += pp[i].cnt;
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message (stderr)

palindrome.cpp: In member function 'PalTree::Node& PalTree::operator[](int)':
palindrome.cpp:58:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<PalTree::Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         if(idx >= pt.size()) {
      |            ~~~~^~~~~~~~~~~~
palindrome.cpp: In function 'int main()':
palindrome.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |  scanf("%s", ss);
      |  ~~~~~^~~~~~~~~~
#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...