标签:二分 str ace char return can int names ems
#include <bits/stdc++.h> using namespace std; int n,k; char str[200005]; int a[300]; int maxsum=0;char zm=0; void return_maxsum_zm(int &maxsum,char &zm) { for(int i=‘a‘;i!=‘z‘+1;i++) { if(maxsum<a[i]) { maxsum=a[i]; zm=i; } } } bool jugde(int mid){ memset(a,0,sizeof(a)); maxsum=0;zm=0; for(int i=0;i<mid;i++) { a[str[i]]+=1; } int left=0,right=mid-1; return_maxsum_zm(maxsum,zm); //printf("%d-%c\n",maxsum,zm); while(right<=n) { a[str[left]]--; left++; right++; a[str[right]]++; //printf("%d\n",right); return_maxsum_zm(maxsum,zm); //printf("%d-%c\n",maxsum,zm); if(maxsum>=k){ //printf("%d,ture\n",mid); return true; } } //printf("%d,false\n",mid); return false; } int main(){ while(~scanf("%d %d",&n,&k)){ getchar(); scanf("%s",str); int l=k; int r=n; int ans=-1; while(l<=r){ int mid=(l+r)/2; if(jugde(mid)){ //printf("%d--%d--%d\n",l,mid,r); ans=mid; r=mid-1; } else{ l=mid+1; } } printf("%d\n",ans); } return 0; }
标签:二分 str ace char return can int names ems
原文地址:https://www.cnblogs.com/MiraculousB/p/12274774.html