답안 #1061973

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1061973 2024-08-16T16:10:58 Z MladenP 회문 (APIO14_palindrome) C++17
100 / 100
32 ms 65372 KB
#include <iostream>
#include <vector>
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
using namespace std;

const int P_SIGMA = 26;
const int MINUS_NODE = 1;
const int ZERO_NODE = 2;

struct PalTree {

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

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 _slink = 0, 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;
    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;
}

Compilation message

palindrome.cpp: In member function 'PalTree::Node& PalTree::operator[](int)':
palindrome.cpp:65:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<PalTree::Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |         if(idx >= pt.size()) {
      |            ~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 360 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 452 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 456 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 456 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2332 KB Output is correct
2 Correct 2 ms 2332 KB Output is correct
3 Correct 2 ms 2332 KB Output is correct
4 Correct 1 ms 2332 KB Output is correct
5 Correct 2 ms 2332 KB Output is correct
6 Correct 2 ms 2660 KB Output is correct
7 Correct 2 ms 2332 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 1536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 16696 KB Output is correct
2 Correct 9 ms 17464 KB Output is correct
3 Correct 7 ms 16696 KB Output is correct
4 Correct 8 ms 17888 KB Output is correct
5 Correct 8 ms 16440 KB Output is correct
6 Correct 7 ms 16704 KB Output is correct
7 Correct 7 ms 16184 KB Output is correct
8 Correct 2 ms 868 KB Output is correct
9 Correct 4 ms 5056 KB Output is correct
10 Correct 7 ms 16908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 63316 KB Output is correct
2 Correct 27 ms 64336 KB Output is correct
3 Correct 32 ms 65100 KB Output is correct
4 Correct 24 ms 64000 KB Output is correct
5 Correct 25 ms 65372 KB Output is correct
6 Correct 22 ms 65084 KB Output is correct
7 Correct 16 ms 33364 KB Output is correct
8 Correct 5 ms 1764 KB Output is correct
9 Correct 5 ms 1764 KB Output is correct
10 Correct 16 ms 34032 KB Output is correct
11 Correct 22 ms 64852 KB Output is correct
12 Correct 7 ms 6188 KB Output is correct