先复制一波题面:
You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K.
For each case, the first line contains a string which only consist of lowercase letters. The second line contains an integer K.
[Technical Specification]
1<=T<= 100
1 <= the length of S <= 100000
1 <= K <= 100000
3 abc 1 abcabc 1 abcabc 2
6 15 21
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
long long t;
cin >> t;
while(t--){
char s[100001];
long long le[27] = {},k;
long long n = 0;
cin >> s >> k;
n = strlen(s);
long long st = 0,en = -1,ds1 = 0,ds2 = 0,res = 0,rc = 0;
while(1){
while(en < n){
if(++le[s[++en] - 'a'] > k ){
break;
}
}
ds1 = en - st;
res += ((ds1 + 1) * ds1) / 2 - ((ds2 + 1) * ds2) / 2;
if(en == n)
break;
while(st < n){
le[s[st++] - 'a']--;
if(le[s[en] - 'a'] <= k){
if(st <= en)
ds2 = en - st;
break;
}
}
}
cout << res << endl;
}
return 0;
}