Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour si, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c — Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer mi and a lowercase letter ci, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
The first line of input contains a positive integer n (1 ≤ n ≤ 1 500) — the length of the garland.
The second line contains n lowercase English letters s1s2... sn as a string — the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 ≤ q ≤ 200 000) — the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer mi (1 ≤ mi ≤ n) — the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter ci — Koyomi's possible favourite colour.
Output q lines: for each work plan, output one line containing an integer — the largest Koyomity achievable after repainting the garland according to it.
6
koyomi
3
1 o
4 o
4 m
3
6
5
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
3
4
5
7
8
1
2
3
4
5
10
aaaaaaaaaa
2
10 b
10 z
10
10
In the first sample, there are three plans:
- In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3is the best achievable;
- In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6;
- In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
hint:给你一个长度为n的字符串,然后有q个询问,每个询问给num个字符c,问你将这num个字符c放进字符串中,能得到最长的连续字符c有多长。。。
看网上大神的代码。。。这个题好像有很多种做法---two points法,dp法...可是我都不会 不过还是好好学习了一波 哈哈
two points法:有时间找专题练练。。。这个方法就是(分成几步来说计较清晰)
①用两个指针 l 和 r,一个指向前,一个指向后
②移动指针r,使得 l 和 r 之间正好用了 num 个 c 字符,然后用 ans 更新答案,
③移动 l 指针使得num = num - 1,然后重复第二步,当 r > n的时候,所有情况都被考虑完了,可以结束循环了
1 //尺取法 --- 要多练练这种题目 2 int solve(char cc){ 3 int l = 0, r = 0, cnt = 0, ans = 0; 4 while(l < n && r < n){ 5 while((cnt < num || chr[r] == cc) && (r < n)){ 6 if(chr[r] != cc) cnt++; 7 r++; 8 } 9 ans = max(ans, r - l); 10 while(chr[l] == cc && l <= r) l++; 11 cnt--; 12 l++; 13 } 14 return ans; 15 } 16 int main(){ 17 cin >> n; 18 cin >> chr; 19 int q; 20 cin >> q; 21 while(q--){ 22 scanf("%d %c", &num, &cc); //要用scanf输入,不要用cin直接 cin >> num >> cc; 23 cout << solve(cc) << endl; 24 } 25 return 0; 26 }
预处理dp法:预处理之后速度好像快了不少。。。这个方法的思想就是先预处理出26种字母的所有可能情况,然后每次询问直接输出就行了
1 //预处理做法 感觉挺快的 2 int main(){ 3 cin >> n; 4 cin >> chr; 5 for(int i=0; i<26; i++){ 6 for(int j=0; j<n; j++){ 7 int cnt = 0; 8 for(int k=j; k<n; k++){ 9 if(chr[k] != c[i]) ++cnt; 10 result[i][cnt] = max(result[i][cnt], k-j+1); 11 } 12 } 13 for(int l=1; l<=n; l++){ 14 result[i][l] = max(result[i][l], result[i][l-1]); 15 } 16 } 17 int q; 18 cin >> q; 19 while(q--){ 20 int x; char c; 21 scanf("%d %c", &x, &c); 22 cout << result[c-'a'][x] << endl; 23 } 24 return 0; 25 }
Clearly, given a color c and a number of repaints m you could bash through all possible intervals and find the maximum valid substring, but this would take too long, with n(n+1)/2 possible intervals and q queries, taking O(n^2*q) time.
We can resolve this issue as there are 26n possible queries (as there are 26 letters in the alphabet and m is constrained to [1,n]. Therefore we run through every possible query and store its answer in an array Ans[c][m]. (this takes O(n^2*26) time)
To run through every possible query we look at every possible color and every single possible subinterval, and find the minimum number of recolors needed to make that interval valid. We compare the size of this interval to the size currently stored in Ans[c][r-l+1-t], and store the maximum of the two.
However, this only finds the maximum value when exactly m colors are recolored, but in some cases not all m colors need to be recolored, for example take string axaxa for favorite color a, a maximum of 2 recolors will be needed even if we have more than 2 recolors available. Therefore, for each color we iterate through [1,m] and if Ans[c][j] is less than Ans[c][j-1], we replace Ans[c][j] with Ans[c][j-1]. Using the above example, Ans[a][2] would = 5 while Ans[a][3] would = 0, and so we set Ans[a][3] to 5. (Restating what I said above: the maximum vaild substring given a number of recolors m is equal to the maximum valid substring that can be obtained by any number p such that 1<=p<=m. We originally only found answers which used exactly m recolors.)
Then we handle each query by using this 2-D array as the solutions are precomputed.
在评论区发现一个老外讲得非常棒啊!就是为什么我们要在预处理之后加一个循环 result[i][j] = max(result[i][j], result[i][j-1]),原因就是,我们前面的做法求的是最少的字符得到的最长的长度,所以很有可能最少字符的 n-xx < n, 这个时候我们如果直接输出result[i][n]的话就会得到0! 所以要处理一番。。。
哇,突然茅塞顿开。。。