Submission #352540

# Submission time Handle Problem Language Result Execution time Memory
352540 2021-01-21T01:22:41 Z mjhmjh1104 Joyful KMP (KRIII5_JK) C++14
7 / 7
559 ms 125892 KB
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

const int MOD = 1e9 + 7;

int uf[1000006], mn[1000006];

int _find(int x) {
    if (uf[x] == -1) return x;
    return uf[x] = _find(uf[x]);
}

void _merge(int x, int y) {
    x = _find(x), y = _find(y);
    if (x == y) return;
    mn[y] = min(mn[x], mn[y]);
    uf[x] = y;
}

int l, res = 1, fail[1000006], order[1000006], t_sz[1000006], used[1000006];
long long k, _r = 1;
char str[1000006], sl[1000006];
vector<int> v, adj[1000006], adj_uf[1000006];
bool visited[1000006], overflow;
long long on[1000006];

void dfs(int x, int sz = 26) {
    visited[x] = true;
    t_sz[x] = sz;
    res = 1LL * res * sz % MOD;
    for (auto &i: adj_uf[x]) if (!visited[i]) dfs(i, sz - 1);
}

int main() {
    scanf("%s%lld", str, &k); k--;
    l = strlen(str);
    for (int i = 0; i < l; i++) uf[i] = -1, mn[i] = i;
    for (int i = 1, j = 0; i < l; i++) {
        while (j > 0 && str[i] != str[j]) {
            adj[i].push_back(j), adj[j].push_back(i);
            j = fail[j - 1];
        }
        if (str[i] == str[j]) {
            _merge(i, j);
            fail[i] = ++j;
        } else adj[i].push_back(j), adj[j].push_back(i);
    }
    for (int i = 0; i < l; i++) v.push_back(_find(i));
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    sort(v.begin(), v.end(), [](const int &a, const int &b) {
        return mn[a] < mn[b];
    });
    for (int i = 0; i < (int)v.size(); i++) order[v[i]] = i;
    for (int i = 0; i < l; i++) for (auto &j: adj[i]) if (order[_find(i)] < order[_find(j)]) adj_uf[_find(i)].push_back(_find(j));
    for (int i = 0; i < l; i++) sort(adj_uf[i].begin(), adj_uf[i].end(), [](const int &a, const int &b) {
        return order[a] < order[b];
    });
    dfs(v[0]);
    printf("%d\n", res);
    reverse(v.begin(), v.end());
    for (auto &i: v) on[i] = -1;
    for (auto &i: v) {
        if (overflow) break;
        on[i] = _r;
        if (__builtin_mul_overflow(_r, t_sz[i], &_r)) overflow = true;
    }
    reverse(v.begin(), v.end());
    if (!overflow && k >= _r) return puts("OVER"), 0;
    for (int i = 0; i < l; i++) visited[i] = false;
    for (auto &j: v) {
        visited[j] = true;
        int s = 0;
        for (int i = 0; i < 26; i++) if (!(used[j] & 1 << i)) {
            if (on[j] == -1 || k - on[j] < 0) {
                sl[j] = 'a' + i;
                s = i;
                break;
            }
            k -= on[j];
        }
        for (auto &i: adj_uf[j]) if (!visited[i]) used[i] |= 1 << s;
    }
    for (int i = 0; i < l; i++) printf("%c", sl[_find(i)]);
}

Compilation message

JK.cpp: In function 'int main()':
JK.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   38 |     scanf("%s%lld", str, &k); k--;
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 29 ms 47340 KB Output is correct
2 Correct 29 ms 47340 KB Output is correct
3 Correct 29 ms 47340 KB Output is correct
4 Correct 30 ms 47468 KB Output is correct
5 Correct 554 ms 125636 KB Output is correct
6 Correct 559 ms 125644 KB Output is correct
7 Correct 505 ms 125612 KB Output is correct
8 Correct 545 ms 125624 KB Output is correct
9 Correct 511 ms 125892 KB Output is correct
10 Correct 30 ms 47340 KB Output is correct
11 Correct 29 ms 47340 KB Output is correct
12 Correct 29 ms 47468 KB Output is correct
13 Correct 31 ms 47724 KB Output is correct
14 Correct 33 ms 47980 KB Output is correct
15 Correct 45 ms 50664 KB Output is correct
16 Correct 61 ms 54372 KB Output is correct
17 Correct 190 ms 80464 KB Output is correct
18 Correct 356 ms 113892 KB Output is correct
19 Correct 370 ms 115300 KB Output is correct
20 Correct 344 ms 114720 KB Output is correct
21 Correct 357 ms 113480 KB Output is correct
22 Correct 368 ms 111820 KB Output is correct
23 Correct 168 ms 67284 KB Output is correct
24 Correct 169 ms 67412 KB Output is correct
25 Correct 186 ms 67284 KB Output is correct
26 Correct 150 ms 67284 KB Output is correct
27 Correct 153 ms 67284 KB Output is correct
28 Correct 149 ms 67284 KB Output is correct
29 Correct 149 ms 67284 KB Output is correct
30 Correct 158 ms 67284 KB Output is correct
31 Correct 161 ms 67412 KB Output is correct
32 Correct 154 ms 67796 KB Output is correct
33 Correct 154 ms 67284 KB Output is correct
34 Correct 151 ms 67284 KB Output is correct
35 Correct 148 ms 67284 KB Output is correct
36 Correct 152 ms 67412 KB Output is correct
37 Correct 163 ms 67796 KB Output is correct
38 Correct 151 ms 67284 KB Output is correct
39 Correct 154 ms 67412 KB Output is correct
40 Correct 154 ms 67296 KB Output is correct
41 Correct 155 ms 67284 KB Output is correct
42 Correct 156 ms 67224 KB Output is correct
43 Correct 153 ms 67412 KB Output is correct
44 Correct 146 ms 67028 KB Output is correct
45 Correct 144 ms 67028 KB Output is correct
46 Correct 155 ms 67028 KB Output is correct
47 Correct 153 ms 67156 KB Output is correct
48 Correct 152 ms 67164 KB Output is correct
49 Correct 86 ms 65244 KB Output is correct
50 Correct 150 ms 67028 KB Output is correct