Codeforces Round #418 C--An impassioned circulation of affection

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.

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.

Examples
input
6
koyomi
3
1 o
4 o
4 m
output
3
6
5
input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
output
3
4
5
7
8
1
2
3
4
5
input
10
aaaaaaaaaa
2
10 b
10 z
output
10
10
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 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.

数据:

1n1500
1≤q≤2000001 \leq q \leq 200\,0001q200000
1≤mi≤n1 \leq m_i \leq n1mi​​n,cic_ici​​ 为小写英文字母

 

题意:

字符串 sss 对于字符 ccc 的权值,定义为 sss 中仅由 ccc 组成的最长连续子串的长度。例如,对于 s=kooomio,其由字符 o 组成的最长连续子串为 ooo,因此它对于字符 o 的权值为 333。

给定由小写字母组成的字符串 sss 以及 qqq 个询问。每个询问形如 (mi,ci)(m_i, c_i)(mi​​,ci​​),表示「求出在 sss 中至多更改 mim_imi​​ 个位置的字符后所得的字符串 s′s's​​ 对于字符 cic_ici​​ 的最大权值」。

 

数据很大,正解是dp,也可以用尺取法 (共同点是我都不会)

乱搞一下 二分检验就过了 

看来他没有卡二分

先统计每个字母的前缀和 

二分mid 判断每个长度为mid的区间是否满足要求

 1 #include <queue>
 2 #include <cctype>
 3 #include <cstdio>
 4 #include <cstring>
 5 
 6 const int MAXN=1510;
 7 
 8 int n,m,k;
 9 
10 int q[30][MAXN];
11 
12 char s[MAXN],x;
13 
14 inline void read(int&x) {
15     int f=1;register char c=getchar();
16     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
17     for(;isdigit(c);x=x*10+c-48,c=getchar());
18     x=x*f;
19 }
20 
21 bool judge(int num) {
22     int p=x-'a'+1;
23     for(int i=1;i<=n-num+1;++i)
24       if(q[p][i+num-1]-q[p][i-1]>=num-k) return false;
25     return true;
26 }
27 
28 int hh() {
29     read(n);
30     scanf("%s",s+1);
31     for(int i=1;i<=n;++i) {
32         for(int j=1;j<=26;++j)
33           q[j][i]=q[j][i-1];
34         ++q[s[i]-'a'+1][i];
35     }
36     read(m);
37     for(int i=1;i<=m;++i) {
38         read(k);scanf("%c",&x);
39         int l=0,r=n+1;
40         while(l+1<r) {
41             int mid=(l+r)>>1;
42             if(judge(mid)) r=mid;
43             else l=mid; 
44         }
45         printf("%d\n",l);
46     }
47     return 0;
48 }
49 
50 int sb=hh();
51 int main(int argc,char**argv) {;}
代码

 

 

posted @ 2017-09-05 11:43  拿叉插猹哈  阅读(241)  评论(0编辑  收藏  举报