Hdu 5056 (Ruler method)

Source: Internet
Author: User

Boring count

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 932 Accepted Submission (s): 382


Problem descriptionyou is given a string S consisting of lowercase letters, and your task is counting the number of subst The ring, the number of each lowercase, the substring is no more than K.

Inputin The first line there was 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

Outputfor, output a line contains the answer.

Sample INPUT3ABC1ABCABC1ABCABC2

Sample Output61521

Source bestcoder Round #11 (Div. 2) Test instructions: Find out the number of substrings in a string that match the number of times each caption appears to be less than K. The number of data reached the 10^5, so O (n^2) certainly not, so we need to use the ruler. The whole process is divided into 4 cloth:

    1. Initialize the left and right endpoints

2. Continue to expand the right endpoint until the condition is met

3. If the condition cannot be met in the second step, the result of the update

4. Expand the left endpoint by 1, then go back to the second step

The process of the ruler is the above, but, for this problem, we have to make a little change, because the condition of the ruler is unable to meet the conditions, but this problem we first expand the right end of the word is one until the condition is not satisfied (find a letter out

now the number of times greater than K Break ), so our condition should be changed to r outer Loop, find the substring that cannot satisfy the condition and then expand the left endpoint until the condition is satisfied. Then what is the number of operator strings? I don't know, either.

, said the discuss district.

The explanation of the teammate:
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<stdlib.h>#include<math.h>using namespaceStd;typedefLong LongLL;Const intN =100005;inthash[n],k;CharStr[n];BOOLjudge () { for(intI=0;i< -; i++){        if(hash[i]>k)return false; }    return true;}intMain () {inttcase; scanf ("%d",&tcase);  while(tcase--) {scanf ("%s", str); scanf ("%d",&k); intLen =strlen (str); memset (Hash,0,sizeof(Hash)); intL=0, r=0; LL CNT=0;  while(r<Len) {Hash[str[r]-'a']++;  while(l<len&&!judge ()) {Hash[str[l]-'a']--; L++; }            if(!judge ()) Break; //printf ("%d%d\n", l,r);CNT =cnt+ (r-l+1); R++; } printf ("%lld\n", CNT); }    return 0;}

Hdu 5056 (Ruler method)

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.