Submission #94435

# Submission time Handle Problem Language Result Execution time Memory
94435 2019-01-18T12:28:08 Z choikiwon Joyful KMP (KRIII5_JK) C++17
2 / 7
282 ms 79420 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double lf;

const int mod = 1e9 + 7;
const int MN = 1000010;

int fa[MN];
void init() {
    for(int i = 0; i < MN; i++) fa[i] = i;
}
int find(int u) {
    if(fa[u] == u) return u;
    else return fa[u] = find(fa[u]);
}
void mrg(int u, int v) {
    u = find(u);
    v = find(v);
    if(u == v) return;
    fa[v] = u;
}

string S;
ll K;
int pi[MN], sol[MN];
vector<int> V[MN], P, more, smul, X;

int main() {
    std::ios::sync_with_stdio(false);

    cin >> S >> K;

    int ans = 26;
    init();
    X.push_back(0);
    P.push_back(26);
    more.push_back(0);
    for(int i = 1, j = 0; i < S.size(); i++) {
        vector<int> chk(26, 0);
        while(j && S[i] != S[j]) {
            if(!chk[ S[j] - 'a' ]) {
                chk[ S[j] - 'a' ] = 1;
                V[i].push_back(j);
            }
            j = pi[j - 1];
        }
        if(S[i] == S[j]) {
            pi[i] = ++j;
            mrg(pi[i] - 1, i);
        }
        else {
            if(!chk[ S[0] - 'a' ]) {
                V[i].push_back(0);
            }
            if(26 - (int)V[i].size() > 1) {
                more.push_back(P.size());
            }
            P.push_back(26 - (int)V[i].size());
            X.push_back(i);
            ans = 1LL * ans * (26 - (int)V[i].size()) % mod;
        }
    }

    cout << ans << endl;

    int pos1 = 0;
    for(int i = 0; i < X.size(); i++) {
        while(pos1 < more.size() && more[pos1] <= i) pos1++;

        vector<int> chk(26, 0);
        for(int j = 0; j < V[ X[i] ].size(); j++) {
            chk[ sol[ find(V[ X[i] ][j]) ] ] = 1;
        }

        for(int j = 0; j < 26; j++) {
            if(chk[j]) continue;

            ll tmp = 1;
            for(int k = pos1; k < more.size(); k++) {
                if((lf)K / tmp <= P[ more[k] ]) {
                    tmp = K;
                    break;
                }
                tmp *= P[ more[k] ];
            }
            if(K > tmp) {
                K -= tmp;
                continue;
            }
            sol[ X[i] ] = j;
            break;
        }
    }

    if(K != 1) {
        cout << "OVER";
        return 0;
    }

    for(int i = 0; i < S.size(); i++) {
        if(pi[i]) sol[i] = sol[ pi[i] - 1 ];
        cout << (char)('a' + sol[i]);
    }
}

Compilation message

JK.cpp: In function 'int main()':
JK.cpp:40:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1, j = 0; i < S.size(); i++) {
                           ~~^~~~~~~~~~
JK.cpp:69:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < X.size(); i++) {
                    ~~^~~~~~~~~~
JK.cpp:70:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(pos1 < more.size() && more[pos1] <= i) pos1++;
               ~~~~~^~~~~~~~~~~~~
JK.cpp:73:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < V[ X[i] ].size(); j++) {
                        ~~^~~~~~~~~~~~~~~~~~
JK.cpp:81:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int k = pos1; k < more.size(); k++) {
                               ~~^~~~~~~~~~~~~
JK.cpp:102:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < S.size(); i++) {
                    ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 27768 KB Output is correct
2 Correct 26 ms 27768 KB Output is correct
3 Correct 28 ms 27768 KB Output is correct
4 Correct 27 ms 27768 KB Output is correct
5 Correct 265 ms 79364 KB Output is correct
6 Correct 279 ms 79388 KB Output is correct
7 Correct 282 ms 79300 KB Output is correct
8 Correct 275 ms 79352 KB Output is correct
9 Correct 278 ms 79420 KB Output is correct
10 Correct 28 ms 27768 KB Output is correct
11 Correct 27 ms 27768 KB Output is correct
12 Correct 23 ms 27824 KB Output is correct
13 Correct 23 ms 27896 KB Output is correct
14 Correct 26 ms 28124 KB Output is correct
15 Correct 32 ms 29432 KB Output is correct
16 Correct 40 ms 31092 KB Output is correct
17 Correct 111 ms 44268 KB Output is correct
18 Correct 197 ms 61160 KB Output is correct
19 Correct 211 ms 60912 KB Output is correct
20 Correct 207 ms 59732 KB Output is correct
21 Correct 194 ms 60652 KB Output is correct
22 Correct 184 ms 57696 KB Output is correct
23 Correct 107 ms 38028 KB Output is correct
24 Correct 117 ms 38144 KB Output is correct
25 Correct 111 ms 38020 KB Output is correct
26 Correct 110 ms 38088 KB Output is correct
27 Correct 106 ms 38036 KB Output is correct
28 Correct 114 ms 38144 KB Output is correct
29 Correct 110 ms 38140 KB Output is correct
30 Correct 103 ms 38144 KB Output is correct
31 Correct 110 ms 38064 KB Output is correct
32 Correct 113 ms 38104 KB Output is correct
33 Correct 112 ms 38016 KB Output is correct
34 Correct 111 ms 38144 KB Output is correct
35 Correct 107 ms 38144 KB Output is correct
36 Correct 109 ms 38164 KB Output is correct
37 Correct 117 ms 38108 KB Output is correct
38 Correct 115 ms 38184 KB Output is correct
39 Correct 106 ms 38144 KB Output is correct
40 Correct 107 ms 38144 KB Output is correct
41 Correct 113 ms 38096 KB Output is correct
42 Correct 111 ms 38140 KB Output is correct
43 Correct 112 ms 38140 KB Output is correct
44 Correct 107 ms 38140 KB Output is correct
45 Correct 110 ms 38144 KB Output is correct
46 Correct 112 ms 38140 KB Output is correct
47 Correct 110 ms 38132 KB Output is correct
48 Correct 107 ms 38144 KB Output is correct
49 Partially correct 110 ms 38144 KB Output is partially correct
50 Correct 109 ms 38056 KB Output is correct