是个dp
dp[i][j] 代表第j次取到右端点是i的情况时长度的最大值
转移方程
dp[i][j]=max(dp[i][j],dp[i-1][j]) 传递最大值
如果合法左端点是ll
dp[i][j]=max(dp[i][j],dp[ll-1][j-1]+(i-ll+1))
就求出右端点是n时的最大值了
//cyc
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mst(a) memset(a,0,sizeof a)
using namespace std;
typedef pair<int,int> pii;
const int maxn=1e7+5;
/*
____ ____ _____ .____________ _____
/_ /_ | / | || ____/_ | / | |
| || |/ | ||____ \ | |/ | |_
| || / ^ / \| / ^ /
|___||___\____ /______ /|___\____ |
|__| \/ |__|
*/
int cnt[26];
int dp[maxn][4];
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,m;
string s1;
cin>>n>>m;
cin>>s1;
s1=" "+s1;
int ll=1;
for(int i=1;i<=n;i++){
for(int j=0;j<=3;j++){
dp[i][j]=max(dp[i-1][j],dp[i][j]);
}
cnt[s1[i]-'a']++;
while(cnt[s1[i]-'a']>m){
cnt[s1[ll]-'a']--;
ll++;
}
for(int j=1;j<=3;j++){
dp[i][j]=max(dp[i][j],dp[ll-1][j-1]+i-ll+1);
}
}
int ans=0;
for(int i=1;i<=3;i++){
ans=max(ans,dp[n][i]);
}
cout<<ans<<endl;
}