Submission #94438

# Submission time Handle Problem Language Result Execution time Memory
94438 2019-01-18T12:33:40 Z choikiwon Joyful KMP (KRIII5_JK) C++17
7 / 7
383 ms 79052 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

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;
        }

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

            ll tmp = 1;
            for(int k = pos1; k < more.size(); k++) {
                ll C = K / tmp;
                if(K % tmp) C++;
                if(C <= P[ more[k] ]) {
                    tmp = K;
                    break;
                }
                tmp *= P[ more[k] ];
            }
            if(K > tmp) {
                K -= tmp;
                continue;
            }
            sol[ X[i] ] = j;
            ok = true;
            break;
        }
        if(!ok) {
            cout << "OVER";
            return 0;
        }
    }

    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:39:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1, j = 0; i < S.size(); i++) {
                           ~~^~~~~~~~~~
JK.cpp:68:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < X.size(); i++) {
                    ~~^~~~~~~~~~
JK.cpp:69:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(pos1 < more.size() && more[pos1] <= i) pos1++;
               ~~~~~^~~~~~~~~~~~~
JK.cpp:72: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:109: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 27 ms 27772 KB Output is correct
2 Correct 28 ms 27768 KB Output is correct
3 Correct 26 ms 27772 KB Output is correct
4 Correct 26 ms 27768 KB Output is correct
5 Correct 365 ms 78940 KB Output is correct
6 Correct 370 ms 79052 KB Output is correct
7 Correct 364 ms 79052 KB Output is correct
8 Correct 383 ms 78928 KB Output is correct
9 Correct 358 ms 78940 KB Output is correct
10 Correct 23 ms 27768 KB Output is correct
11 Correct 22 ms 27768 KB Output is correct
12 Correct 23 ms 27768 KB Output is correct
13 Correct 29 ms 27896 KB Output is correct
14 Correct 29 ms 28124 KB Output is correct
15 Correct 34 ms 29432 KB Output is correct
16 Correct 45 ms 30960 KB Output is correct
17 Correct 136 ms 43800 KB Output is correct
18 Correct 245 ms 60752 KB Output is correct
19 Correct 262 ms 60416 KB Output is correct
20 Correct 253 ms 59456 KB Output is correct
21 Correct 255 ms 60380 KB Output is correct
22 Correct 259 ms 57288 KB Output is correct
23 Correct 113 ms 37632 KB Output is correct
24 Correct 111 ms 37756 KB Output is correct
25 Correct 105 ms 37756 KB Output is correct
26 Correct 104 ms 37768 KB Output is correct
27 Correct 104 ms 37604 KB Output is correct
28 Correct 105 ms 37628 KB Output is correct
29 Correct 115 ms 37724 KB Output is correct
30 Correct 112 ms 37628 KB Output is correct
31 Correct 114 ms 37628 KB Output is correct
32 Correct 116 ms 37752 KB Output is correct
33 Correct 114 ms 37664 KB Output is correct
34 Correct 110 ms 37664 KB Output is correct
35 Correct 113 ms 37720 KB Output is correct
36 Correct 106 ms 37756 KB Output is correct
37 Correct 103 ms 37632 KB Output is correct
38 Correct 105 ms 37640 KB Output is correct
39 Correct 106 ms 37744 KB Output is correct
40 Correct 106 ms 37760 KB Output is correct
41 Correct 105 ms 37724 KB Output is correct
42 Correct 105 ms 37688 KB Output is correct
43 Correct 113 ms 37632 KB Output is correct
44 Correct 116 ms 37828 KB Output is correct
45 Correct 109 ms 37652 KB Output is correct
46 Correct 113 ms 37732 KB Output is correct
47 Correct 111 ms 37760 KB Output is correct
48 Correct 114 ms 37716 KB Output is correct
49 Correct 89 ms 32816 KB Output is correct
50 Correct 111 ms 37680 KB Output is correct