杭电5672

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

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

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

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<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
char ch[1000005];
int vis[200];
int main(){
    int t;
    cin>>t;
    while(t--){
        scanf("%s",ch);
        int k;
        scanf("%d",&k);
        long long ans=0;
        memset(vis,0,sizeof(vis));
        int sum=1,len=strlen(ch),l=0,r=0;
        vis[ch[l]]++;
        if(sum>=k)ans+=len-r;
        for(int i=1;i<len;i++){
            if(!vis[ch[i]])sum++;
            vis[ch[i]]++;
            r=i;
            while(sum>=k){
                ans+=len-i;
                vis[ch[l]]--;
                if(!vis[ch[l]])sum--;
                l++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

你可能感兴趣的:(杭电5672)