D - String暴力超时版

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?
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).
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

#include<stdio.h>
#include<string.h>
#define Maxn 1000001
int main(){
	int time;
	int count;
	int num;
	int i, j, k, n, len, t, m, dif, zu;
	int start = 0;
	long  ans; 
	char a[Maxn];
	int b[26] = {0};
	scanf("%d", &time);
	while(time--){
		ans = 0;
		scanf("\n");
		gets(a);
		scanf("\n");
		scanf("%d", &dif);
		len = strlen(a);
	//	printf("len = %d\n", len);
	//	for(i = 0; i < len; i++){
	//		b[a[i] - 'a'] = 1;	
	//	}
	//	for(i = 0; i < 26; i++){
	//		printf("b[%d] = %d\n", i, b[i]);
	//	}
	//	printf("\n");
		for(k = dif; k <= len; k++){
				for(i = start; i + k <= len; i++){
				memset(b, 0, sizeof(b));
				count = 0;
				j = k;
				t = i;
				//printf("i = %d i + k - 1 = %d k = %d\n", i, i + k - 1, k);
				while(j--){
					//printf("j = %d t = %d num = %d\n", j, t, a[t] - 'a');
					b[a[t] - 'a']++;
					//printf("%d \n", t);
					t++;
				}
				for(m = 0; m < 26; m++){
					//printf("b[%d] = %d\n", m, b[m]);
					if(b[m] != 0){
						//printf("b[%d] = %d\n", m, b[m]);
						count++;
					}
				}
				//printf("count = %d dif = %d\n", count, dif);
				if(count >= dif){
					ans ++;
					//printf("ans = %d\n");
				}
				//printf("\n");
			}
		}
		printf("%d\n", ans);

		//printf("%d ",time);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41895253/article/details/82914950
今日推荐