# hdu5672 string(尺取法)

108人阅读 评论(0)

String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1332 Accepted Submission(s): 445

Problem Description
There is a string S.S only contain lower case English character.(10≤length(S)≤1,000,000)
How many substrings there are that contain at least k(1≤k≤26) distinct characters?

Input
There are multiple test cases. The first line of input contains an integer T(1≤T≤10) indicating the number of test cases. For each test case:

The first line contains string S.
The second line contains a integer k(1≤k≤26).

Output
For each test case, output the number of substrings that contain at least k dictinct characters.

Sample Input

2
abcabcabca
4
abcabcabcabc
3

Sample Output

0
55

#include<cstdio>
#include<cstring>

const int maxn=1000005;
char a[maxn];
int dig[27];

int main() {
int T;
scanf("%d",&T);
while(T--) {
int k;
memset(a,0,sizeof(a));
scanf("%s%d",a,&k);
int len=strlen(a);
long long ans=0;
memset(dig,0,sizeof(dig));
int s=0,t=-1,num=0;
while(t<len&&s<len) {
while(num<k&&(++t)<len) {
if(!dig[a[t]-'a'])++num;
++dig[a[t]-'a'];
}
if(num==k)
ans+=(len-t);
if(--dig[a[s]-'a']==0)--num;
++s;
}
printf("%lld\n",ans);
}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：70219次
• 积分：4314
• 等级：
• 排名：第6988名
• 原创：357篇
• 转载：6篇
• 译文：0篇
• 评论：30条
博客专栏
 博弈论入门 文章：17篇 阅读：6588
 湖南省第九届省赛 文章：7篇 阅读：2921
评论排行
最新评论