String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1961 Accepted Submission(s): 636
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
Recommend
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char s[1000333];
int c[30];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
memset(c,0,sizeof(c));
int len=strlen(s);
int l = 0,r = 0,k;
long long ans=0;
scanf("%d",&k);
int cnt=0;
while(l<=r&&l<len)
{
while(cnt<k&&r<len)
{
c[s[r]-'a']++;
if(c[s[r]-'a']==1)cnt++;
r++;
}
if(cnt==k)ans+=len-r+1;
if(c[s[l]-'a']==1)
cnt--;
c[s[l]-'a']--;
l++;
}
printf("%lld\n",ans);
}
}
本文介绍了一个关于字符串的编程挑战,任务是找出含有至少k种不同字符的所有子串数量。通过输入一系列测试案例,文章展示了如何使用滑动窗口算法解决此问题,并提供了完整的代码实现。
779

被折叠的 条评论
为什么被折叠?



