답안 #941993

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
941993 2024-03-10T01:03:03 Z JoseSoto 회문 (APIO14_palindrome) C++14
100 / 100
35 ms 63172 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) ((int) (x).size())
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vp = vector<pii>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vb = vector<bool>;

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p){return os << '(' << p.fi << ", " << p.se << ')';}
template<typename C, typename T = typename enable_if<!is_same<C, string>::value, typename C::value_type>::type>
ostream& operator<<(ostream &os, const C &v){string sep; for(const T &x : v) os << sep << x, sep = " "; return os;}
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values){
    cout << "[Debug]\n\t" << vars << " = ";
    string d = "[";
    (..., (cout << d << values, d = "] ["));
    cout << "]\n";
}

constexpr short alpha = 26;
constexpr char offset = 'a';

struct state{
    int l, link, cnt;
    array<int,alpha> go;
    state(){
        l = cnt = 0;
        go.fill(0);
    }
};

struct eertree{
    int n = 2;
    int last = 1;
    vector<state> et;
    string s;

    eertree(){
        et.resize(2);
        et[0].link = et[1].link = 1;
        et[1].l = -1;
    }

    int palSuff(int x){
        while(s[sz(s) - 2 - et[x].l] != s.back()) x = et[x].link;
        return x;
    }

    int add(char ch){
        s.pb(ch);
        last = palSuff(last);
        bool new_pal = !et[last].go[ch-offset];
        if(new_pal){
            et.pb(state());
            et[last].go[ch-offset] = n++;
            et.back().link = et[palSuff(et[last].link)].go[ch-offset];
            et.back().l = et[last].l + 2;
            if(et.back().l == 1) et.back().link = 0;
        }
        last = et[last].go[ch-offset];
        // Do something with last, maybe if new_pal
        et[last].cnt++;
        if(et[last].l == sz(s)) last = et[last].link;
        return new_pal;
    }

    ll dp(){
        ll ans = 0;
        for(int i=n-1; i>=0; i--){
            et[ et[i].link ].cnt += et[i].cnt;
            ans = max(ans, 1LL*et[i].l*et[i].cnt);
        }
        return ans;
    }
};

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);

    string s;
    cin >> s;
    eertree et;
    for(auto ch: s) et.add(ch);
    cout << et.dp() << "\n";
    return 0;
}

Compilation message

palindrome.cpp: In function 'void logger(std::string, Args&& ...)':
palindrome.cpp:27:42: warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17'
   27 |     (..., (cout << d << values, d = "] ["));
      |                                          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 460 KB Output is correct
18 Correct 1 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 1 ms 348 KB Output is correct
22 Correct 1 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 1 ms 456 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 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 468 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 756 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 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 2364 KB Output is correct
2 Correct 2 ms 2364 KB Output is correct
3 Correct 2 ms 2364 KB Output is correct
4 Correct 2 ms 2364 KB Output is correct
5 Correct 2 ms 2364 KB Output is correct
6 Correct 2 ms 2476 KB Output is correct
7 Correct 2 ms 2364 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 1552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 16944 KB Output is correct
2 Correct 9 ms 15920 KB Output is correct
3 Correct 8 ms 16320 KB Output is correct
4 Correct 8 ms 17452 KB Output is correct
5 Correct 9 ms 16432 KB Output is correct
6 Correct 8 ms 16428 KB Output is correct
7 Correct 8 ms 17456 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 5 ms 4744 KB Output is correct
10 Correct 10 ms 16812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 61380 KB Output is correct
2 Correct 27 ms 63052 KB Output is correct
3 Correct 25 ms 62792 KB Output is correct
4 Correct 26 ms 61276 KB Output is correct
5 Correct 26 ms 62876 KB Output is correct
6 Correct 24 ms 63172 KB Output is correct
7 Correct 19 ms 32340 KB Output is correct
8 Correct 5 ms 1888 KB Output is correct
9 Correct 5 ms 1764 KB Output is correct
10 Correct 19 ms 31580 KB Output is correct
11 Correct 26 ms 62796 KB Output is correct
12 Correct 8 ms 5252 KB Output is correct