답안 #94436

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
94436 2019-01-18T12:31:41 Z choikiwon Joyful KMP (KRIII5_JK) C++17
2 / 7
296 ms 79020 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long 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] ] + 1e-12) {
                    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++) {
                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 27772 KB Output is correct
2 Correct 27 ms 27896 KB Output is correct
3 Correct 23 ms 27768 KB Output is correct
4 Correct 29 ms 27768 KB Output is correct
5 Correct 259 ms 78904 KB Output is correct
6 Correct 263 ms 79020 KB Output is correct
7 Correct 280 ms 78908 KB Output is correct
8 Correct 283 ms 78972 KB Output is correct
9 Correct 296 ms 79000 KB Output is correct
10 Correct 26 ms 27768 KB Output is correct
11 Correct 27 ms 27768 KB Output is correct
12 Correct 27 ms 27768 KB Output is correct
13 Correct 28 ms 27896 KB Output is correct
14 Correct 28 ms 28028 KB Output is correct
15 Correct 37 ms 29432 KB Output is correct
16 Correct 46 ms 30956 KB Output is correct
17 Correct 126 ms 43816 KB Output is correct
18 Correct 227 ms 60952 KB Output is correct
19 Correct 238 ms 60396 KB Output is correct
20 Correct 205 ms 59244 KB Output is correct
21 Correct 216 ms 60332 KB Output is correct
22 Correct 223 ms 57320 KB Output is correct
23 Correct 105 ms 37632 KB Output is correct
24 Correct 113 ms 37696 KB Output is correct
25 Correct 111 ms 37684 KB Output is correct
26 Correct 110 ms 37688 KB Output is correct
27 Correct 111 ms 37604 KB Output is correct
28 Correct 111 ms 37712 KB Output is correct
29 Correct 107 ms 37628 KB Output is correct
30 Correct 110 ms 37704 KB Output is correct
31 Correct 102 ms 37760 KB Output is correct
32 Correct 109 ms 37760 KB Output is correct
33 Correct 110 ms 37888 KB Output is correct
34 Correct 101 ms 37792 KB Output is correct
35 Correct 100 ms 37620 KB Output is correct
36 Correct 100 ms 37712 KB Output is correct
37 Correct 101 ms 37672 KB Output is correct
38 Correct 100 ms 37732 KB Output is correct
39 Correct 103 ms 37748 KB Output is correct
40 Correct 107 ms 37632 KB Output is correct
41 Correct 108 ms 37664 KB Output is correct
42 Correct 104 ms 37640 KB Output is correct
43 Correct 99 ms 37712 KB Output is correct
44 Correct 100 ms 37632 KB Output is correct
45 Correct 102 ms 37676 KB Output is correct
46 Correct 109 ms 37628 KB Output is correct
47 Correct 100 ms 37648 KB Output is correct
48 Correct 110 ms 37620 KB Output is correct
49 Partially correct 109 ms 37632 KB Output is partially correct
50 Correct 106 ms 37628 KB Output is correct