用数组a[26]记录每次字母出现的次数,用j标记起点坐标,对于每个字母a[i],以他结尾并且符合条件的字符串有i-j个
然后从第一个字母开始,但重复字母等于k的时候,将从j开始找第一个a[j]=a[i]
每次ans+=(i-j)
#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#include<cmath>
#include<functional>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)
#define inf 1000000000+7
using namespace std;
typedef long long ll;
int main()
{
int T;
cin >> T;
while (T--)
{
int a[26] = { 0 };
char s[100000 + 20];
scanf("%s", &s[0]);
int k;
cin >> k;
ll ans = 0, cot = 0;
int j = 0;
for (int i = 0; s[i]; i++)
{
if (a[s[i] - 'a'] == k)
{
while (s[j] != s[i])
{
a[s[j] - 'a']--;
j++;
cot--;
}
a[s[j] - 'a']--;
j++;
cot--;
}
a[s[i] - 'a']++;
cot++;
ans += cot;
}
printf("%lld\n", ans);
}
return 0;
}