题干:
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.
Input
In the first line there is an integer T , indicates the number of test cases.
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
Output
For each case, output a line contains the answer.
Sample Input
3
abc
1
abcabc
1
abcabc
2
Sample Output
6
15
21
题目大意:
找出字符串中所有字母出现次数不超过k次的子串数量
解题报告:
尺取就可以了。
AC代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
char a[100000 + 5];
int bk[40];
int k;
ll ans;
int main()
{
// freopen("in4.txt","r",stdin);
int t,l,r,len;
cin>>t;
while(t--) {
memset(bk,0,sizeof(bk) );
ans = 0;
scanf("%s",a);
scanf("%d",&k);
len = strlen(a);
l=r=0;
while(r<len) {
bk[a[r]-'a']++;
while(bk[ a[r] - 'a' ] > k) {//注意这是while不是if
bk[ a[l] - 'a']--;
l++;
}
// printf("%lld %d %d \n",ans,l,r);
ans+=r-l+1;
r++;
}
printf("%lld\n",ans);
}
/** //以左端点
while(r<len) {
if(l == r) {
r++;
}
}
//后续处理
while(l<=r) {
}
*/
return 0 ;
}