0%

Stringforces

Stringforces

time limit per test: 3 seconds
memory limit per test: 256 megabytes

Articles

You are given a string ss of length nn. Each character is either one of the first kk lowercase Latin letters or a question mark.

You are asked to replace every question mark with one of the first kk lowercase Latin letters in such a way that the following value is maximized.

Let fif_i be the maximum length substring of string ss, which consists entirely of the ii-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the ii-th letter doesn’t appear in a string, then fif_i is equal to 00.

The value of a string ss is the minimum value among fif_i for all ii from 11 to kk.

What is the maximum value the string can have?

Input

The first line contains two integers nn and k(1n2105;1k17)k (1≤n≤2⋅105; 1≤k≤17) — the length of the string and the number of first Latin letters used.

The second line contains a string ss, consisting of nn characters. Each character is either one of the first kk lowercase Latin letters or a question mark.

Output

Print a single integer — the maximum value of the string after every question mark is replaced with one of the first kk lowercase Latin letters.

Examples

input

1
2
10 2
a??ab????b

output

1
4

input

1
2
9 4
?????????

output

1
2

input

1
2
2 3
??

output

1
0

input

1
2
15 3
??b?babbc??b?aa

output

1
3

input

1
2
4 4
cabd

output

1
1

Note

In the first example the question marks can be replaced in the following way: "aaaababbbb""aaaababbbb". f1=4,f2=4f_1=4, f_2=4, thus the answer is 44. Replacing it like this is also possible: "aaaabbbbbb""aaaabbbbbb". That way f1=4,f2=6f_1=4, f_2=6, however, the minimum of them is still 44.

In the second example one of the possible strings is "aabbccdda""aabbccdda".

In the third example at least one letter won’t appear in the string, thus, the minimum of values fi is always 00.

Tutorial

code

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
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>

using namespace std;

const int maxn = 2e5+1;
int suf[maxn][18], dp[1<<17];
int n, m;
string s;

bool check(int len) {
for(int i = 1; i <= m; ++ i) {
suf[n][i] = n+1;
}
int k = 0, kk = 0, id = -1, kkk = 0;
for(int i = n; i >= 1; -- i) {
for(int j = 1; j <= m; ++ j) {
suf[i-1][j] = suf[i][j];
}
if (id == -1 || s[i-1] == '?' || s[i-1] - 'a' + 1 == id) {
if (s[i-1] == '?') {
kk ++;
if (i == n || s[i] == '?') {
if (++ kkk >= len) {
for(int j = 1; j <= m; ++ j) {
suf[i-1][j] = i + len - 1;
}
}
}
else kkk = 1;
}
else {
kkk = 0;
if (++ k == 1) {
id = s[i-1] - 'a' + 1;
}
}
if (id != -1 && k + kk >= len) {
suf[i-1][id] = i + len - 1;
}
}
else {
k = 1;
kk = kkk;
kkk = 0;
id = s[i-1] - 'a' + 1;
if (k + kk >= len) {
suf[i-1][id] = i + len -1;
}
}
}
// for(int i = 1; i <= n; ++ i) {
// cout << suf[i-1][4] << " \n"[i==n];
// }
int inf = n+1, ans = inf;
for(int i = 1; i < (1<<m); ++ i) {
dp[i] = n+1;
}
for(int i = 0; i < (1<<m); ++ i) {
if (dp[i] == inf) continue;
int cnt = 0;
for(int j = 0; j < m; ++ j) {
if (i >> j & 1) continue;
cnt ++;
dp[i+(1<<j)] = min(dp[i+(1<<j)], suf[dp[i]][j+1]);
}
if (cnt == 0) ans = min(dp[i], ans);
}
return ans < inf;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> s;
int l = 1, r = n + 1;
// cout << check(3) << "\n";
while(l < r) {
int mid = l + r >> 1;
if (check(mid)) l = mid + 1;
else r = mid;
}
cout << l - 1 << "\n";
return 0;
}
-------------本文结束感谢您的阅读-------------