1. 程式人生 > >5056】Boring count (尺取法)

5056】Boring count (尺取法)

題幹:

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

解題報告:

    尺取就可以了。

AC程式碼:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

char a[100000 + 5];
int bk[40];
int k;
ll ans;
int main()
{
//	freopen("in4.txt","r",stdin);
	int t,l,r,len;
	cin>>t;
	while(t--) {
		memset(bk,0,sizeof(bk) );
		ans = 0;	
		scanf("%s",a);
		scanf("%d",&k);
		len = strlen(a);
		l=r=0;
		while(r<len) {
			bk[a[r]-'a']++;
			while(bk[ a[r] - 'a' ] > k) {
				bk[ a[l] - 'a']--;
				l++;
			}
//			printf("%lld  %d  %d  \n",ans,l,r);
			ans+=r-l+1;
			r++; 
		}
		printf("%lld\n",ans);
	}
/**		//以左端點 
		while(r<len) {
			if(l == r) {
				r++;
			}
		}
		//後續處理 
		while(l<=r) {
			
		}
*/		
	return 0 ;
}