HDOJ 5672//Simulation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.