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

Nadeko为了庆祝哥哥Koyomi的生日,计划通过重新涂色纸花环中的部分花瓣来最大化哥哥最喜欢的花瓣颜色的连续出现次数。面对不确定的工作量和哥哥的喜好,Nadeko制定了多种方案来寻找最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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) {;}
代码

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值