제출 #1061964

#제출 시각아이디문제언어결과실행 시간메모리
1061964MladenP회문 (APIO14_palindrome)C++17
15 / 100
2 ms1508 KiB
#include <iostream>
#include <vector>
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
using namespace std;
const int P_SIGMA = 26;
const int MINUS_NODE = 0;
const int ZERO_NODE = 1;

struct PalTree {



struct Node {
    int len, cnt, dub, slink;
    int to[P_SIGMA];
    Node() { len = cnt = dub = 0; slink = -1; for(int i = 0; i < P_SIGMA; i++) to[i] = 0; }
    Node(int _len, int _slink = -1, int _cnt = 0, int _dub = 0) {
        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;

void init() {
    s = "";
    suff = 2;
    pt.push_back(Node(-1, MINUS_NODE)); 
    pt.push_back(Node(0, MINUS_NODE)); 
}

    int toidx(char c) { return c-'a'; }
    int 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));
            if(suff == MINUS_NODE) {
                pt[nd].slink = ZERO_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;
        return pt[nd].dub;
    }
    void calcCnt() {
        for(int i = pt.size()-1; i > 2; i--) {
            pt[pt[i].slink].cnt += pt[i].cnt;
        }
    }

    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);
    }
    pt.calcCnt();
    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);
    }
    cout << rez << endl;
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

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