您的位置:首页 > 其它

hdu5672 string(尺取法)

2016-06-01 21:36 363 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: