Boring count
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 705 Accepted Submission(s): 288
Problem Description
You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K.
Input
In the first line there is an integer T , indicates the number of test cases.
For each case, the first line contains a string which only consist of lowercase letters. The second line contains an integer K.
[Technical Specification]
1<=T<= 100
1 <= the length of S <= 100000
1 <= K <= 100000
Output
For each case, output a line contains the answer.
Sample Input
3 abc 1 abcabc 1 abcabc 2
Sample Output
6 15 21
/*
HDU 5056 字符串处统计
统计子串的个数,要求子串中的字符重复次数不大于K (<=)
hash统计次数 小写的字符97开始 ,26个
刚开始用O(N^2)超时
for(i=0;i<len-1;i++)
{
memset(hash,0,sizeof(hash));
hash[str[i]-'a']=1;
for(j=i+1;j<len;j++)
{
hash[str[j]-'a']++;
if(hash[str[j]-'a']>k)
break;
sum++;
}
}
看了题解;
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define N 100010
char str[N];
int hash[30];
int main()
{
int t,k,i,len;
__int64 sum,pre;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
scanf("%d",&k);
len=strlen(str);
sum=0;
pre=0;
memset(hash,0,sizeof(hash));
for(i=0;i<len;i++)
{
hash[ str[i]-'a' ]++;
while(hash[ str[i]-'a' ]>k)
{
hash[ str[pre]-'a' ]--;
pre++;
}
sum+=i-pre+1;
}
printf("%I64d\n",sum);
}
return 0;
}