-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathD.cpp
75 lines (74 loc) · 1.71 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define int128 __int128_t
const int N = 4e5 + 5;
const ull P = 131;
int n, d, id;
char ch[N], ans[N];
ull hash1[N], hash2[N], f[N];
bool is;
inline void init()
{
f[0] = 1; for (register int i = 1; i < N; i++) f[i] = f[i - 1] * P;
}
inline bool check(int l, int r)
{
return (hash1[r] - hash1[l - 1] * f[r - l + 1]) * f[l - 1] == hash2[r] - hash2[l - 1];
}
inline void ___()
{
init();
scanf("%lld", &d);
scanf("%s", ch + 1);
n = strlen(ch + 1);
id = n;
for (register int i = 1; i <= n; i++)
{
hash1[i] = hash1[i - 1] * P + ch[i];
hash2[i] = hash2[i - 1] + ch[i] * f[i - 1];
if (i >= d && check(i - d + 1, i)) { id = i; break; }
if (i > d && check(i - d, i)) { id = i; break; }
ans[i] = ch[i];
}
for (register int i = id; i >= 1; i--)
{
is = false;
for (register char j = ch[i] + 1; j <= 'z'; j++)
{
hash1[i] = hash1[i - 1] * P + j;
hash2[i] = hash2[i - 1] + j * f[i - 1];
if (i >= d && check(i - d + 1, i)) continue;
if (i > d && check(i - d, i)) continue;
ans[i] = j;
id = i;
is = true;
break;
}
if (is) break;
}
if (!is) return void(printf("Impossible\n"));
for (register int i = id + 1; i <= n; i++)
{
is = false;
for (register char j = 'a'; j <= 'z'; j++)
{
hash1[i] = hash1[i - 1] * P + j;
hash2[i] = hash2[i - 1] + j * f[i - 1];
if (i >= d && check(i - d + 1, i)) continue;
if (i > d && check(i - d, i)) continue;
ans[i] = j;
is = true;
break;
}
if (!is) return void(printf("Impossible\n"));
}
printf("%s\n", ans + 1);
}
signed main()
{
register int t = 1;
// scanf("%lld", &t);
while (t--) ___();
}