#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int t, ch, l, r;
long long ans, len, k;
string s;
bool flag;
scanf("%d", &t);
while(t--)
{
ans = 0;
cin >> s >> k;
len = s.size();
int sum[len + 5][27];
memset(sum, 0, sizeof(sum));
for(int i = 0; i < len; i++)
{
ch = s[i] - 'a';
for(int j = 0; j < 26; j++)
{
sum[i + 1][j] = sum[i][j];
}
sum[i + 1][ch]++;
}
l = 0;
r = 1;
while(r <= len)
{
flag = 0;
for(int i = 0; i < 26; i++)
{
if(sum[r][i] - sum[l][i] > k)
{
flag = 1;
break;
}
}
if(flag)
{
l++;
}
else
{
ans += r - l;
r++;
}
}
printf("%lld\n", ans);
}
return 0;
}
AC用时327ms,贴一个用时15ms代码的博客https://blog.csdn.net/u012860063/article/details/39674597