Codeforces-An impassioned circulation of affection (动态规划/尺取法)

来源:互联网 发布:矩阵的实际应用 编辑:程序博客网 时间:2024/06/11 16:03

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 Koyomityachievable after repainting the garland according to each plan.

Input

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

Output q lines: for each work plan, output one line containing an integer — the largest Koyomity achievable after repainting the garland according to it.

Example
Input
6koyomi31 o4 o4 m
Output
365
Input
15yamatonadeshiko101 a2 a3 a4 a5 a1 b2 b3 b4 b5 b
Output
3457812345
Input
10aaaaaaaaaa210 b10 z
Output
1010
Note

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 3 is 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.

第一种方法

Time
(ms)Mem
(MB)Length15752.2691

/*题意:输入:n-字符串长度;接下来一个字符串,然后有一个q表示q次询问,最后有两个数num表示可以替换的字符数量,str表示要找的最长连续字符长度字符。题解:尺取法,每次更新最大长度即可。*/#include <cstdio>    #include <cmath>    #include <iostream>    #include<algorithm>#include<string.h>using namespace std;int main(){int n;int q;char str[1505];while (cin >> n){memset(dp, 0, sizeof(dp));cin >> str;cin >> q;int ans = 0;char st;int num;for (int i = 0; i < q; i++){scanf("%d %c", &num, &st);ans = 0;//答案int cnt = 0;//已经使用的次数int kk = 0;for (int j = 0; j < n; j++){if (str[j] != st)cnt++;while (cnt > num){if (str[kk] != st){cnt--;}kk++;}ans = max(ans, j - kk + 1);}cout << ans << endl;}}return 0;}

第二种方法:DP+尺取法思想。

/*题解:dp[k][num]表示当关键颜色为 k+'a' 时,替换num个字符的连续子串的最大长度。*/#include <cstdio>    #include <cmath>    #include <iostream>    #include<algorithm>#include<string.h>using namespace std;int dp[27][1505];int main(){int n;int q;char str[1505];while (cin >> n){memset(dp, 0, sizeof(dp));cin >> str;cin >> q;for (int k = 0; k < 26; k++)//26个字母{for (int i = 0; i < n; i++){int num = 0;for (int j = i; j < n; j++){if (str[j] != k + 'a'){num++;}dp[k][num] = max(dp[k][num], j - i + 1);}}for (int j = 1; j <= n; j++)//替换两个能达到的长度,那么替换三个也能达到,那么就需要更新{dp[k][j] = max(dp[k][j], dp[k][j - 1]);}}char sss;int nn;while (q--){cin >> nn >> sss;cout << dp[sss - 'a'][nn] << endl;}}return 0;}







阅读全文
0 0
原创粉丝点击