# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
22845 | past future present (#40) | Joyful KMP (KRIII5_JK) | C++14 | 1000 ms | 10880 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<stdio.h>
#include<string.h>
using ll = long long;
const int mod = 1e9 + 7;
char s[1000005], ans[1000005];
int n, ff[1000005];
void calculate_pi() {
ff[0] = -1;
for (int i = 1, j = -1; i < n; i++) {
while (j >= 0 && s[i] != s[j + 1]) j = ff[j];
if (s[i] == s[j + 1])
ff[i] = ++j;
else
ff[i] = -1;
}
}
int pt[1000005], psz;
int main()
{
ll k;
scanf("%s%lld", s,&k);
n = strlen(s);
int num = 26;
for (int i = 1, j=-1; i < n; i++){
char orig = s[i];
int origj = j;
while(j >=0 && s[i] != s[j+1]) j=ff[j];
if(s[i]==s[j+1]) ff[i] = ++j;
else ff[i] = -1;
int afterj = j;
if(ff[i] == -1){
ll c = 0;
for(int ii=0;ii<26;ii++){
s[i] = 'a'+ii;
j = origj;
while(j >=0 && s[i] != s[j+1]) j=ff[j];
if(s[i]==s[j+1]) ff[i] = ++j;
else ff[i] = -1;
if(ff[i] == -1)c++;
}
num = num*c%mod;
pt[++psz] = i;
}
j = afterj;
s[i] = orig;
ff[i] = -1;
}
printf("%d\n", num);
k--;
for (int j = psz; j >= 0; j--)
{
ans[pt[j]] = 'a' + k % 25;
k /= 25;
}
if (k > 0)
{
puts("OVER");
return 0;
}
for (int i = 1; i < n; i++)
{
if (ans[i])
{
if (ans[i] >= ans[0]) ans[i]++;
continue;
}
ans[i] = ans[ff[i]];
}
puts(ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |