#include<bits/stdc++.h>
#define N 400100
using namespace std;
typedef unsigned long long ULL;
const ULL MOD=29;
ULL cf[N],a[N];
int n,m;
char s[N];
inline void ex(){
puts("Impossible");
exit(0);
}
ULL h[N];
void dfs(int p,int f,ULL h1,ULL h2){
if(p==n+1){
for(int i=1;i<=n;i++) printf("%c",a[i]+'a'-1);
puts("");
exit(0);
}
if(f==0) a[p]=1;
else a[p]=s[p]-'a'+1;
ULL nh1,nh2;
while(a[p]<=26){
if(p>m) nh1=(h1-a[p-m]*cf[m-1])*MOD+a[p];
else nh1=h1*MOD+a[p];
nh2=h2+cf[p-1]*a[p];
h[p]=nh2;
if((p<m||nh1*cf[p-m]!=h[p]-h[p-m])&&(p<m+1||(a[p-m]*cf[m-1]*MOD+nh1)*cf[p-m-1]!=h[p]-h[p-m-1])) dfs(p+1,a[p]+'a'-1==s[p]&&f,nh1,nh2);
a[p]++;
}
}
int main(){
scanf("%d%s",&m,s+1);
n=strlen(s+1);
s[n]++;
for(int i=n;i;i--){
if(s[i]>'z') s[i]='a',s[i-1]++;
else break;
}
if(s[0]) ex();
cf[0]=1;
for(int i=1;i<=n;i++) cf[i]=cf[i-1]*MOD;
dfs(1,1,0,0);
ex();
return 0;
}
Codeforces Round #124 (Div. 1) D. The Next Good String
最新推荐文章于 2024-07-09 12:20:48 发布