mymilkbottles

越努力,越幸运! 以现在大多数人的努力程度之低,根本轮不到可以拼天赋。...

hdu5672 string(尺取法)

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;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mymilkbottles/article/details/51559271
个人分类: ACM_模拟
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

hdu5672 string(尺取法)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭