# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
94435 | choikiwon | Joyful KMP (KRIII5_JK) | C++17 | 282 ms | 79420 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |