# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
94426 | choikiwon | Joyful KMP (KRIII5_JK) | C++17 | 12 ms | 8184 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;
const int mod = 1e9 + 7;
const int MN = 1000010;
void getPi(string S, int *pi) {
for(int i = 1, j = 0; i < S.size(); i++) {
while(j && S[i] != S[j]) j = pi[j - 1];
if(S[i] == S[j]) {
pi[i] = ++j;
}
}
}
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;
}
int po[MN];
string S;
ll K;
int pi[MN], sol[MN];
vector<int> V;
int main() {
std::ios::sync_with_stdio(false);
po[0] = 1;
for(int i = 1; i < MN; i++) {
po[i] = 1LL * po[i - 1] * 25 % mod;
}
cin >> S >> K;
getPi(S, pi);
init();
for(int i = 0; i < S.size(); i++) {
if(pi[i]) {
mrg(pi[i] - 1, i);
}
}
for(int i = 0; i < S.size(); i++) if(fa[i] == i) V.push_back(i);
int ans = 1LL * 26 * po[ (int)V.size() - 1 ] % mod;
cout << ans << endl;
for(int i = 0; i < V.size(); i++) {
for(int j = 0; j < 26; j++) {
if(i && j == sol[ V[0] ]) continue;
if(K > po[ (int)V.size() - 1 - i ]) {
K -= po[ (int)V.size() - 1 - i ];
continue;
}
sol[ V[i] ] = j;
break;
}
}
if(K != 1) {
cout << "OVER";
return 0;
}
for(int i = 0; i < S.size(); i++) {
cout << (char)('a' + sol[ fa[i] ]);
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |