HDU 5672:String【模拟】

String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 892    Accepted Submission(s): 289


Problem Description
There is a string  S . S  only contain lower case English character. (10length(S)1,000,000)
How many substrings there are that contain at least  k(1k26)  distinct characters?
 

Input
There are multiple test cases. The first line of input contains an integer  T(1T10)  indicating the number of test cases. For each test case:

The first line contains string  S .
The second line contains a integer  k(1k26) .
 

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
醉了,G++超时,C++过了。。。。
AC-code:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int T,k,len,i,j,kk,vis[27];
	long long sum;
	char str[1000005];
	scanf("%d",&T);
	while(T--)
	{
		cin>>str;
		cin>>k;
		len=strlen(str);
		if(k==1)
		{
			cout<<(long long)(len+1)*len/2<<endl;
			continue;
		}
		kk=0;
		sum=0;
		memset(vis,0,sizeof(vis));
		j=0;
		for(i=0;i<len;i++)
		{
			if(!vis[str[i]-'a'])
				kk++;
			vis[str[i]-'a']++;
			while(kk>=k)
			{
				sum+=len-i;
				vis[str[j]-'a']--;
				if(!vis[str[j]-'a'])
				{
					kk--;
					j++;
					break;
				}
				j++;
			}
		}
		cout<<sum<<endl;
	}
	return 0;
 } 


你可能感兴趣的:(HDU 5672:String【模拟】)