hdu 5056(尺取法思路題)

來源:互聯網
上載者: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 are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K. 

 

InputIn the first line there is 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 each case, output a line contains the answer. 

 

Sample Input3abc1abcabc1abcabc2 

 

Sample Output61521 

 

Source BestCoder Round #11 (Div. 2)  題意:找出一個字串裡面符合每個字幕出現次數都不大於K次的子串的個數。題解:資料量達到了10^5,所以O(n^2)肯定不行,所以要用到尺取法。整個過程分為4布:

    1.初始化左右端點

    2.不斷擴大右端點,直到滿足條件

    3.如果第二步中無法滿足條件,則終止,否則更新結果

    4.將左端點擴大1,然後回到第二步

尺取法的過程是上述,但是,對於這題,我們要做少許改動,因為尺取法的條件終止條件是無法滿足條件,但是這題我們首先擴充右端點的話是一直到不滿足條件(找到某個字母出

現次數大於K的那個串再break),所以這題我們的條件應該改成r在外層迴圈,找到無法滿足條件的子串後再一直擴充左端點,直到滿足條件。接下來怎麼運算元串個數呢?我也不知道

,discuss區裡面這樣說的。。

隊友的解釋:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <stdlib.h>#include <math.h>using namespace std;typedef long long LL;const int N = 100005;int Hash[N],k;char str[N];bool judge(){    for(int i=0;i<26;i++){        if(Hash[i]>k) return false;    }    return true;}int main(){    int tcase;    scanf("%d",&tcase);    while(tcase--){        scanf("%s",str);        scanf("%d",&k);        int len = strlen(str);        memset(Hash,0,sizeof(Hash));        int l=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(尺取法思路題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.