Problem Description
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
分析:
枚举左端点尺取
ps:
简单尺取
很快写完结果细节没处理好过不去样例,一度怀疑自己写错了
浪费好多时间找bug
码一下细节防止再犯
code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
#define int long long
const int maxm=1e5+5;
char s[maxm];
int mark[26];
int n,k;
void init(){
memset(mark,0,sizeof mark);
}
signed main(){
int T;
scanf("%lld",&T);
while(T--){
init();
scanf("%s%lld",s+1,&k);
n=strlen(s+1);
int r=1;
int sum=0;
int ans=0;
for(int i=1;i<=n;i++){//枚举左端点
while(r<=n&&sum==0){//找到不满足条件的右边界
int t=s[r++]-'a';
mark[t]++;
if(mark[t]==k+1){//超过k
sum++;
}
}
if(sum){//如果不满足,则右端点不能加进去
ans+=(r-1-1)-i+1;//r要减1才是当前的右端点,找了好久才找到这个地方错了
}else{//如果满足,则右端点可以加进去
ans+=(r-1)-i+1;
}
int t=s[i]-'a';//左端点移动一格
mark[t]--;
if(mark[t]==k){//不超过k
sum--;
}
}
printf("%lld\n",ans);
}
return 0;
}