#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;
}
for(int j = 0; j < 26; j++) {
if(chk[j]) continue;
ll tmp = 1;
for(int k = pos1; k < more.size(); k++) {
if((K + tmp - 1) / 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: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:80:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = pos1; k < more.size(); k++) {
~~^~~~~~~~~~~~~
JK.cpp:101: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 |
27896 KB |
Output is correct |
2 |
Correct |
27 ms |
27712 KB |
Output is correct |
3 |
Correct |
26 ms |
27768 KB |
Output is correct |
4 |
Correct |
27 ms |
27768 KB |
Output is correct |
5 |
Correct |
405 ms |
78936 KB |
Output is correct |
6 |
Correct |
385 ms |
79056 KB |
Output is correct |
7 |
Correct |
389 ms |
78904 KB |
Output is correct |
8 |
Correct |
388 ms |
79032 KB |
Output is correct |
9 |
Correct |
393 ms |
79012 KB |
Output is correct |
10 |
Correct |
21 ms |
27768 KB |
Output is correct |
11 |
Correct |
23 ms |
27896 KB |
Output is correct |
12 |
Correct |
22 ms |
27740 KB |
Output is correct |
13 |
Correct |
24 ms |
27896 KB |
Output is correct |
14 |
Correct |
29 ms |
28076 KB |
Output is correct |
15 |
Correct |
36 ms |
29436 KB |
Output is correct |
16 |
Correct |
44 ms |
30956 KB |
Output is correct |
17 |
Correct |
142 ms |
43848 KB |
Output is correct |
18 |
Correct |
250 ms |
60776 KB |
Output is correct |
19 |
Correct |
267 ms |
60520 KB |
Output is correct |
20 |
Correct |
256 ms |
59244 KB |
Output is correct |
21 |
Correct |
257 ms |
60476 KB |
Output is correct |
22 |
Correct |
246 ms |
57320 KB |
Output is correct |
23 |
Correct |
110 ms |
37756 KB |
Output is correct |
24 |
Correct |
108 ms |
37756 KB |
Output is correct |
25 |
Correct |
113 ms |
37728 KB |
Output is correct |
26 |
Correct |
108 ms |
37632 KB |
Output is correct |
27 |
Correct |
110 ms |
37732 KB |
Output is correct |
28 |
Correct |
108 ms |
37668 KB |
Output is correct |
29 |
Correct |
110 ms |
37708 KB |
Output is correct |
30 |
Correct |
109 ms |
37668 KB |
Output is correct |
31 |
Correct |
113 ms |
37760 KB |
Output is correct |
32 |
Correct |
108 ms |
37756 KB |
Output is correct |
33 |
Correct |
112 ms |
37760 KB |
Output is correct |
34 |
Correct |
107 ms |
37632 KB |
Output is correct |
35 |
Correct |
114 ms |
37660 KB |
Output is correct |
36 |
Correct |
107 ms |
37632 KB |
Output is correct |
37 |
Correct |
108 ms |
37604 KB |
Output is correct |
38 |
Correct |
108 ms |
37728 KB |
Output is correct |
39 |
Correct |
108 ms |
37716 KB |
Output is correct |
40 |
Correct |
109 ms |
37632 KB |
Output is correct |
41 |
Correct |
110 ms |
37604 KB |
Output is correct |
42 |
Correct |
107 ms |
37756 KB |
Output is correct |
43 |
Correct |
101 ms |
37744 KB |
Output is correct |
44 |
Correct |
102 ms |
37760 KB |
Output is correct |
45 |
Correct |
108 ms |
37700 KB |
Output is correct |
46 |
Correct |
103 ms |
37632 KB |
Output is correct |
47 |
Correct |
109 ms |
37776 KB |
Output is correct |
48 |
Correct |
110 ms |
37712 KB |
Output is correct |
49 |
Partially correct |
107 ms |
37616 KB |
Output is partially correct |
50 |
Correct |
104 ms |
37756 KB |
Output is correct |