Source: http: // 218.194.91.48/acmhome/problemdetail. do? & Method = showdetail & id = 1456
Description
Bruce force's keyboard is broken, only a few keys are still working. Bruce has figured out he can still type texts by switching the keyboard layout whenever
He needs to type a letter which is currently not mapped to any ofMWorking keys of the keyboard.
You are given a text that Bruce wishes to type, and he asks you if you can tell him the maximum number of consecutive characters in the text which can be typed without having to switch the keyboard layout. for simplicity,
We assume that each key of the keyboard will be mapped to exactly one character, and it is not possible to type other characters by combination of different keys. this means that Bruce wants to know the length of the largest substring of the text which consists
Of at mostMDifferent characters.
Input
The input contains several test cases, each test case consisting of two lines. The first line of each test case contains the numberM(1
≤ MB ≤ 128), Which specifies how many keys on the keyboard are still working. the second line of each test case contains the text which Bruce wants to type. you may assume that the length of this text does not exceed 1 million characters. note that
Input may contain space characters, which shoshould be handled like any other character.
The last test case is followed by a line containing one zero.
Output
For each test case, print one line with the length of the largest substring of the text which consists of at mostMDifferent
Characters.
Sample Input
5
This can't be solved by brute force.
1
Mississippi
0
Sample output
7
2
Prompt
Hint: the largest substring for the first test case is "_ by_bru", where _ stands for a space character.
// Comment out the specific ideas of this question here, and warn you that you cannot calculate the speed of the question, but also consider how to do it and what the method is. This requires time to exercise .. // This question is: give you a string, and then ask you how many consecutive strings can contain up to n different characters .. # include <cstdio> # include <cstring> # include <iostream> using namespace STD; char a [1000005]; int T1, T2, L, R, N, res, Len; int B [500]; int main () {While (scanf ("% d", & N) {memset (B, 0, sizeof (B )); getchar (); gets (a); Len = strlen (a); T1 = t2 = res = 0; L = r = 0; while (L <= R & R <Len) {// interval while (t1 <= N & R <Len) {// calculate the number of different characters <specified number .. if (B [A [R] = 0) {// if no character exists, mark B [A [R] = 1; T1 ++; if (T1> N) break;} else B [A [R] ++; r ++; T2 ++; // Add the right boundary} If (T2> res) res = t2; // calculate the maximum value if (r> = Len) break; // jump out of the loop while (1) {B [A [l] --; if (B [A [l] = 0) break; l ++; T2 --;} l ++; T1 --; r ++ ;} printf ("% d \ n", Res);} return 0 ;}