# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
958019 | puelwon | Joyful KMP (KRIII5_JK) | C++17 | 1 ms | 2140 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>
#include <vector>
using namespace std;
#define mod 1000000007
long long int f(int n)
{
long long int sum=1;
for(int i=26;i>26-n;i--)
{
sum=(sum*i)%mod;
}
return sum;
}
int main(void)
{
char str[1000001];
(void)scanf("%s",str);
long long int k;
(void)scanf("%lld",&k);
int ar[27]={};
int er[27]={};
int unused=0;
vector<int> we[27];
int len=strlen(str);
for(int i=0;i<len;i++)
{
if(er[str[i]-'a']==0)
{
er[str[i]-'a']=1;
ar[str[i]-'a']=unused;
we[unused++].push_back(i);
}
else
{
we[ar[str[i]-'a']].push_back(i);
}
}
long long int cnt=f(unused);
long long int cc=26-(unused-1);
int re[27]={};
k--;
for(int i=unused-1;i>=0;i--)
{
re[i]=k%cc;
k/=cc;
cc++;
}
if(k!=0)
{
printf("%lld\n",cnt);
printf("OVER");
return 0;
}
char ans[1000001]={};
for(int i=0;i<unused;i++)
{
int pp=re[i]+'a';
int tt[27+'a']={};
for(int k=0;k<i;k++)
{
tt[(int)ans[k]]=1;
}
for(int k='a';k<=pp;k++)
{
if(tt[k]==1)
pp++;
}
for(int j=0;j<we[i].size();j++)
{
ans[we[i][j]]=pp;
}
}
printf("%lld\n",cnt);
printf("%s",ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |