尺取
复杂度 (n)
1 # include <cstdio> 2 # include <iostream> 3 # include <algorithm> 4 # include <cstring> 5 using namespace std; 6 7 const int maxn=1500+5; 8 int n,m; 9 char s[maxn]; 10 int d; 11 char ss[3]; 12 13 int main(){ 14 while(scanf("%d",&n)!=EOF){ 15 scanf("%s",s); 16 scanf("%d",&m); 17 while(m--){ 18 scanf("%d%s",&d,ss); 19 int res=0,ls=0,rs=0,sum=0; 20 for(ls=0;ls<n;ls++){ 21 while(rs<n&&sum+(s[rs]!=ss[0])<=d) { 22 sum+=(s[rs]!=ss[0]); 23 rs++; 24 } 25 res=max(res,rs-ls); 26 sum-=(s[ls]!=ss[0]); 27 } 28 printf("%d ",res); 29 } 30 } 31 return 0; 32 }