codeforce—An impassioned circulation of affection11.30training
Problem - 814C - Codeforces
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.
娜迪科的生日快到了!在她为派对布置房间的时候,一个由石竹花形纸片组成的长长的花环被放在了墙壁的显眼处。小富哥哥一定会喜欢的!
Nadeko 对花环还不满意,决定再打磨一下。花环上有 n 个纸片,从左到右依次编号为 1 至 n , i 至 si 个纸片的颜色为 si ,用小写英文字母表示。Nadeko 将重新绘制最多 m 个棋子,给每个棋子涂上任意的新颜色(仍然用小写英文字母表示)。完成这项工作后,她会找出花环中所有只包含小美哥哥最喜欢的颜色 c 的小段,并将其中最长的长度作为花环的 小美度。
例如,假设花环用"kooomo"表示,而小美哥哥最喜欢的颜色是"o"。在所有只包含"o"的子线段中,"ooo"是最长的,长度为 3 。因此,这个花环的 Koyomity 等于 3 。
但是问题来了,因为 Nadeko 不确定 Koyomi 哥哥最喜欢的颜色,而且对工作量的想法也摇摆不定。她对此有 q 个计划,每个计划都可以用一对整数 mi 和一个小写字母 ci 来表示,其含义已在上面解释。您要找出根据每个计划重新绘制花环后可实现的最大 Koyomity 值。
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.
输入
第一行输入包含一个正整数 n ( 1 ≤ n ≤ 1 500 ) - 花环的长度。
第二行包含 n 个小写英文字母 s1s2... sn --花环上纸片的初始颜色。
第三行包含一个正整数 q ( 1 ≤ q ≤ 200 000 ) - Nadeko 的计划数。
接下来的 q 行分别描述了一个计划:其中第 i 行包含一个整数 mi ( 1 ≤ mi ≤ n )--需要重新绘制的纸片的最大数量,接着是一个空格,然后是一个小写英文字母 ci --Koyomi可能最喜欢的颜色。
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.
输出
输出 q 行:对于每个工作计划,输出一行,其中包含一个整数--根据该计划重新绘制花环后可实现的最大 Koyomity 值。
Examples
15yamatonadeshiko101 a2 a3 a4 a5 a1 b2 b3 b4 b5 b
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.
注意
在第一个示例中,有三个计划:
- 在第一个方案中,最多可以重新绘制 1 块。将"y"重新绘制成"o"的结果是"kooomi",其Koyomity为 3 ,这是可以实现的最佳结果;
- 在第二个方案中,最多可以重新绘制 4 个棋子,而"oooooo"的结果是共通性为 6 ;
- 在第三个方案中,最多可以重新绘制 4 个棋子,而"mmmmmi"和"kmmmmm"都会导致Koyomity为 5 。
题解
- 尺取法
观察本题数据 暴力的对每次询问直接查找也是可以过得,所以我们用r,l来维护一个在改变不超过k个字母的前提下,所能达到区间的左右端点,同时注意我们在r每一次移动都要保证一个结果的最大值。代码如下:
tip: 尺取法,用处还是蛮大的,挖个坑去练题
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;cin>>n;
string s;cin>>s;
int t;cin>>t;int res = 0;
while(t--){
int k;cin>>k;char m;cin>>m;
int l =0,r=0;
int len = 0;
res = 0;
for(int r = 0;r < n;r ++){
if(s[r] != m)
len ++;
while(len > k){
if(s[l] != m)len--;
l++;
}
res = max(res,r - l + 1);
}
cout<<res<<endl;
}
return 0;
}
- 离线dp
我们发现对于每一次询问都要查找,这显然是很慢的,而且英文字母只有26个我们可以进行一次完全查找,使得最后的询问达到O(1)复杂度,完全查找的复杂度是O(26*n*n),dp[a][b]表示的是,对于字母b,在可操作a个字符的情况下,所能达到的最大长度。我们只需要遍历每一种取法,保留最大值即可。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1500 + 10;
char s[maxn];
int dp[maxn][30];
int main(){
int n;
scanf("%d",&n);
scanf("%s", s);
for (int i = 0; i < 26; ++i){
for (int j = 0; j < n; ++j){
int sum = 0;
for (int k = j; k < n; ++k){
if (s[k] == i+'a')++sum;
dp[k-j+1-sum][i] = max(dp[k-j+1-sum][i], k-j+1);
}
}
}
int q;
scanf("%d",&q);
char cmd[3];
while(q--){
int x,y;
scanf("%d%s",&x, cmd);
int ans = 0;
for (int i = 0; i <= x; ++i){
ans = max(ans, dp[i][cmd[0]-'a' ]);
}
printf("%d\n", ans);
}
return 0;
}

浙公网安备 33010602011771号