Submission #395572

# Submission time Handle Problem Language Result Execution time Memory
395572 2021-04-28T15:34:03 Z Victor Palindromes (APIO14_palindrome) C++17
100 / 100
49 ms 47048 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)

#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back

#define umap unordered_map
#define uset unordered_set

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;

const int INF = 1000000007;

vi dummy(26, -1);

struct node {
    int suflink;
    vi next = dummy;
    ll len;
};

string str;
ll ans = 0;
int currnode = 0, nodes = 1, cnt[300003];
node tree[300003];

void insertNode(int pos) {
    int cur = currnode, curlen = 0;
    int let = str[pos] - 'a';

    while (true) {
        curlen = tree[cur].len;
        if (pos - 1 - curlen >= 0 && str[pos - 1 - curlen] == str[pos])
            break;
        cur = tree[cur].suflink;
    }
    if (tree[cur].next[let] != -1) {
        currnode = tree[cur].next[let];
        return;
    }

    currnode=++nodes;
    tree[nodes].len = tree[cur].len + 2;
    tree[cur].next[let] = nodes;

    if (tree[currnode].len == 1) {
        tree[currnode].suflink = 1;
        return;
    }
    
    while (true) {
        cur = tree[cur].suflink;
        curlen = tree[cur].len;
        if (pos - 1 - curlen >= 0 && str[pos - 1 - curlen] == str[pos]) {
            tree[nodes].suflink = tree[cur].next[let];
            break;
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    memset(cnt, 0, sizeof(cnt));
    cin >> str;

    node root;
    root.len = -1;
    root.suflink = 0;
    tree[0] = root;

    root.len = 0;
    tree[1] = root;

    rep(i, 0, sz(str)) {
        insertNode(i);
        ++cnt[currnode];
    }

    per(i, 2, nodes+1) {
        ans = max(ans, tree[i].len * ll(cnt[i]));
        cnt[tree[i].suflink] += cnt[i];
    }
    cout << ans << endl;
    return 0;
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(i, a, b) for (int i = a; i < (b); ++i)
      |                                        ^
palindrome.cpp:87:5: note: in expansion of macro 'rep'
   87 |     rep(i, 0, sz(str)) {
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 38 ms 46020 KB Output is correct
2 Correct 37 ms 46088 KB Output is correct
3 Correct 36 ms 46048 KB Output is correct
4 Correct 42 ms 46036 KB Output is correct
5 Correct 36 ms 46028 KB Output is correct
6 Correct 35 ms 46028 KB Output is correct
7 Correct 35 ms 46044 KB Output is correct
8 Correct 35 ms 46084 KB Output is correct
9 Correct 36 ms 46004 KB Output is correct
10 Correct 36 ms 46028 KB Output is correct
11 Correct 36 ms 46100 KB Output is correct
12 Correct 41 ms 46008 KB Output is correct
13 Correct 36 ms 46032 KB Output is correct
14 Correct 37 ms 46080 KB Output is correct
15 Correct 36 ms 45988 KB Output is correct
16 Correct 35 ms 46020 KB Output is correct
17 Correct 36 ms 46052 KB Output is correct
18 Correct 37 ms 46028 KB Output is correct
19 Correct 36 ms 46048 KB Output is correct
20 Correct 36 ms 46092 KB Output is correct
21 Correct 44 ms 46088 KB Output is correct
22 Correct 36 ms 46028 KB Output is correct
23 Correct 35 ms 45996 KB Output is correct
24 Correct 36 ms 46108 KB Output is correct
25 Correct 37 ms 46004 KB Output is correct
26 Correct 37 ms 46060 KB Output is correct
27 Correct 45 ms 45976 KB Output is correct
28 Correct 37 ms 46048 KB Output is correct
29 Correct 37 ms 46020 KB Output is correct
30 Correct 46 ms 46020 KB Output is correct
31 Correct 36 ms 46020 KB Output is correct
32 Correct 36 ms 46012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 45996 KB Output is correct
2 Correct 36 ms 46052 KB Output is correct
3 Correct 37 ms 45992 KB Output is correct
4 Correct 37 ms 46072 KB Output is correct
5 Correct 36 ms 45976 KB Output is correct
6 Correct 39 ms 46032 KB Output is correct
7 Correct 37 ms 46000 KB Output is correct
8 Correct 36 ms 46028 KB Output is correct
9 Correct 37 ms 46028 KB Output is correct
10 Correct 37 ms 45984 KB Output is correct
11 Correct 36 ms 46080 KB Output is correct
12 Correct 36 ms 46028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 46136 KB Output is correct
2 Correct 41 ms 46064 KB Output is correct
3 Correct 41 ms 46020 KB Output is correct
4 Correct 48 ms 46092 KB Output is correct
5 Correct 41 ms 46096 KB Output is correct
6 Correct 43 ms 46104 KB Output is correct
7 Correct 45 ms 46028 KB Output is correct
8 Correct 39 ms 46020 KB Output is correct
9 Correct 42 ms 46020 KB Output is correct
10 Correct 40 ms 46040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 46404 KB Output is correct
2 Correct 39 ms 46340 KB Output is correct
3 Correct 40 ms 46384 KB Output is correct
4 Correct 41 ms 46404 KB Output is correct
5 Correct 43 ms 46368 KB Output is correct
6 Correct 40 ms 46416 KB Output is correct
7 Correct 40 ms 46364 KB Output is correct
8 Correct 43 ms 46328 KB Output is correct
9 Correct 41 ms 46356 KB Output is correct
10 Correct 40 ms 46404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 46972 KB Output is correct
2 Correct 49 ms 47040 KB Output is correct
3 Correct 45 ms 46968 KB Output is correct
4 Correct 45 ms 46932 KB Output is correct
5 Correct 45 ms 47040 KB Output is correct
6 Correct 45 ms 46960 KB Output is correct
7 Correct 49 ms 47028 KB Output is correct
8 Correct 44 ms 47048 KB Output is correct
9 Correct 45 ms 46996 KB Output is correct
10 Correct 44 ms 46928 KB Output is correct
11 Correct 46 ms 47040 KB Output is correct
12 Correct 45 ms 46996 KB Output is correct