답안 #395759

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
395759 2021-04-28T21:56:23 Z SavicS 회문 (APIO14_palindrome) C++14
100 / 100
32 ms 37496 KB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 300005;
const int inf = 1e9 + 5;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

int n;
string s;

struct palindromic_tree {
    int N;
    int idx;
    int last;
    int a[maxn];
    int len[maxn];
    int cnt[maxn];
    int fail[maxn];
    int lenj[maxn];
    int son[maxn][26];

    int new_node(int X) {
        memset(son[idx], 0, sizeof(son[idx]));
        len[idx] = X;
        cnt[idx] = 0;
        return idx++;
    }

    void init() {
        N = 0, last = 0, idx = 0;
        new_node(0), new_node(-1);
        fail[0] = 1;
        a[0] = -1;
    }

    int get_fail(int X) {
        while(a[N - len[X] - 1] != a[N])X = fail[X];
        return X;
    }

    void add(int X) {
        X -= 'a';
        a[++N] = X;

        int cur = get_fail(last);
        if(!son[cur][X]) {
            int nw = new_node(len[cur] + 2);
            fail[nw] = son[get_fail(fail[cur])][X];
            son[cur][X] = nw;
        }

        last = son[cur][X];
        lenj[last] += 1;
    }

    void update() {
        fb(i,idx - 1, 0) {

			if(lenj[i] == 0)continue;

            int X = i;
            while(X != 0) {
                cnt[X] += lenj[X];
                
				lenj[fail[X]] += lenj[X];
				lenj[X] = 0;
				
                X = fail[X];
            }

        }
    }

} P;

int main() 
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> s;
    n = sz(s);

    P.init();
    ff(i,0,n - 1)P.add(s[i]);
    P.update();

    ll rez = 0;
    ff(i,0,P.idx - 1)rez = max(rez, 1ll * P.len[i] * P.cnt[i]);
    cout << rez << endl;

    return 0;
}
/**

abacaba

www

// probati bojenje sahovski ili slicno

**/


# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 288 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 1 ms 1484 KB Output is correct
3 Correct 1 ms 1484 KB Output is correct
4 Correct 1 ms 1484 KB Output is correct
5 Correct 2 ms 1484 KB Output is correct
6 Correct 2 ms 1612 KB Output is correct
7 Correct 1 ms 1484 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12620 KB Output is correct
2 Correct 9 ms 12756 KB Output is correct
3 Correct 8 ms 12748 KB Output is correct
4 Correct 9 ms 12728 KB Output is correct
5 Correct 12 ms 12748 KB Output is correct
6 Correct 8 ms 9612 KB Output is correct
7 Correct 8 ms 10956 KB Output is correct
8 Correct 3 ms 1100 KB Output is correct
9 Correct 5 ms 3792 KB Output is correct
10 Correct 9 ms 10956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 37072 KB Output is correct
2 Correct 23 ms 37388 KB Output is correct
3 Correct 24 ms 37496 KB Output is correct
4 Correct 23 ms 37412 KB Output is correct
5 Correct 32 ms 37392 KB Output is correct
6 Correct 24 ms 33492 KB Output is correct
7 Correct 22 ms 31416 KB Output is correct
8 Correct 6 ms 2388 KB Output is correct
9 Correct 8 ms 2388 KB Output is correct
10 Correct 26 ms 30940 KB Output is correct
11 Correct 24 ms 37452 KB Output is correct
12 Correct 8 ms 5588 KB Output is correct