【HDU - 5056 】【Boring count】

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/82055538

题目:

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

题意:给我们一个字符串和一个数字k,其中每个字母出现的次数是不能超过k次的,问有多少种字符串,。、

解题思路:暴力求解,若加入一个新的字母后,没有超过k就直接加上长度就行,若达到k,需要向前移动,直到找到不让那个字母的次数超过k的位置才停下。

ac代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 101015
using namespace std;
char str[maxn];
int prime[27];

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int k;
		memset(prime,0,sizeof(prime));
		scanf("%s",str);
		scanf("%d",&k);
		long long int ans=0;
		int st=0;
		for(int i=0;str[i];i++)
		{
			int pos=str[i]-'a';
			prime[pos]++;
			if(prime[pos]>k)
			{
				while(str[st]!=str[i])
				{
					prime[str[st]-'a']--;
					st++;
				}
				prime[str[st]-'a']--;
				st++;
			}
			ans+=(i-st+1);
		}
		printf("%lld\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/82055538