답안 #551614

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
551614 2022-04-21T07:39:40 Z hoanghq2004 회문 (APIO14_palindrome) C++14
100 / 100
24 ms 36272 KB
#include <bits/stdc++.h>
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

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

const int N = 3e5 + 10;

int nNode, suff;
string s;

struct node {
    int nxt[26];
    int len, link;
} Tree[N];

void init() {
    nNode = 1, suff = 1;
    Tree[0].len = -1;
    Tree[1].len = 0;
}

void add(int i) {
    int cur = suff;
    while (1) {
        int curlen = Tree[cur].len;
        if (s[i] == s[i - curlen - 1]) break;
        cur = Tree[cur].link;
    }
    if (Tree[cur].nxt[s[i] - 'a']) {
        suff = Tree[cur].nxt[s[i] - 'a'];
        return;
    }
    suff = ++nNode;
    Tree[cur].nxt[s[i] - 'a'] = suff;
    Tree[suff].len = Tree[cur].len + 2;
    if (Tree[suff].len == 1) {
        Tree[suff].link = 1;
        return;
    }
    while (1) {
        cur = Tree[cur].link;
        int curlen = Tree[cur].len;
        if (s[i] == s[i - curlen - 1]) break;
    }
    Tree[suff].link = Tree[cur].nxt[s[i] - 'a'];
}

long long cnt[N];

int main() {
    ios :: sync_with_stdio(0); cin.tie(0);
    cin >> s;
    init();
    for (int i = 0; i < s.size(); ++i) add(i), ++cnt[suff];
    for (int i = nNode; i >= 0; --i)
        cnt[Tree[i].link] += cnt[i];
    long long ans = 0;
    for (int i = 1; i <= nNode; ++i) ans = max(ans, cnt[i] * Tree[i].len);
    cout << ans;
}

Compilation message

palindrome.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("O3")
      | 
palindrome.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      | 
palindrome.cpp: In function 'int main()':
palindrome.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i = 0; i < s.size(); ++i) add(i), ++cnt[suff];
      |                     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 328 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 340 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 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12376 KB Output is correct
2 Correct 8 ms 12372 KB Output is correct
3 Correct 8 ms 12372 KB Output is correct
4 Correct 8 ms 12272 KB Output is correct
5 Correct 7 ms 12372 KB Output is correct
6 Correct 6 ms 9172 KB Output is correct
7 Correct 8 ms 10580 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 4 ms 3284 KB Output is correct
10 Correct 7 ms 10584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 36184 KB Output is correct
2 Correct 24 ms 36140 KB Output is correct
3 Correct 23 ms 36256 KB Output is correct
4 Correct 23 ms 36168 KB Output is correct
5 Correct 23 ms 36188 KB Output is correct
6 Correct 21 ms 32348 KB Output is correct
7 Correct 21 ms 30296 KB Output is correct
8 Correct 7 ms 1244 KB Output is correct
9 Correct 7 ms 1248 KB Output is correct
10 Correct 22 ms 29832 KB Output is correct
11 Correct 21 ms 36272 KB Output is correct
12 Correct 9 ms 4324 KB Output is correct