String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 630 Accepted Submission(s): 198
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?
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) .
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
Source
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
using namespace std;
const int maxn=1000010;
int vis[30];
char str[maxn];
int main()
{
int i,j,k,n,m,t;
scanf("%d",&t);
while(t--){
scanf("%s%d",str,&k);
memset(vis,0,sizeof(vis));
int len=strlen(str);
long long temp=0,ans=0,sum=0,r=-1;
for(i=0;i<len;++i){
while(temp!=k&&r<len){
r++;
if(r>=len)break;
if(vis[str[r]-'a']==0)temp++;
vis[str[r]-'a']++;
}if(r>=len)break;
ans=ans+len-r;
if(--vis[str[i]-'a']==0)temp--;
}
printf("%lld\n",ans);
}
return 0;
}