Title: http://acm.hdu.edu.cn/showproblem.php?pid=5672
Test instructions: There is a string s, the string contains only lowercase letters, ask how many substrings have at least k different letters;
Idea: or put in the code inside said will be better, in fact, is to maintain a left endpoint and satisfy the nature of the smallest right end of the process.
1#include <stdio.h>2#include <iostream>3#include <stdlib.h>4#include <algorithm>5#include <string.h>6 #defineN 10001007 CharS[n];8 intVis[n];9 intT;Ten intK; One intMain () A { -scanf"%d",&t); - while(t--) the { -scanf"%s", s); -scanf"%d",&k); - intlen=strlen (s); +memset (Vis,0,sizeof(VIS));//mark the number of occurrences of a letter - Long Long intCnt=0, ans=0, len1=-1, sum=0; + for(intI=0; i<len;i++) A { at while(cnt!=k&&len1<len) - { -len1++; - if(len1>=len) - Break; - if(vis[s[len1]-'a']==0) incnt++;//each with a different then the counter adds a -vis[s[len1]-'a']++; to + } - if(Len1>=len)//jump out when you're done enumerating the Break; *ans=ans+ (LEN-LEN1);//if (i,j) satisfies this property, then (I,k) (k>=j) satisfies this property $ if(--vis[s[i]-'a']==0)//If the letter satisfies this character only once, then the letter must not satisfy the character, that is, the different letters will be one less, it is necessary to re-find the smallest right end of the satisfied naturePanax Notoginsengcnt--; - } theprintf"%lld\n", ans); + } A return 0; the}
View Code
HDOJ 5672//Simulation