hdu 5056Boring count

题目链接:http://acm.hdu.edu.cn/showproblem.php?

pid=5056

题目大意:就是问在子串中每一个小写字母出现次数不超过k次的个数,注意子串是连续的子串。。

思路:

code:

<span style="font-size:18px;">#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>

using namespace std;

char str[100010];
int cnt[30];
int main()
{
    int T,k,i;
    scanf("%d",&T);
    while(T--)
    {
        memset(str,0,sizeof(str));
        memset(cnt,0,sizeof(cnt));
        scanf("%s%d",str,&k);
        int len=strlen(str);
        int starPos=0;
        __int64 ans=0;
        for(i=0;i<len;i++)
        {
            cnt[str[i]-'a']++;
            if(cnt[str[i]-'a']>k)
            {
                while(str[starPos]!=str[i])
                {
                    cnt[str[starPos]-'a']--;
                    starPos++;
                }
                cnt[str[starPos]-'a']--;
                starPos++;
            }
            printf("AAA %d %d
",i,starPos);
            ans+=(i-starPos+1);
        }
        printf("%I64d
",ans);
    }
    return 0;
}
</span>


原文地址:https://www.cnblogs.com/zfyouxi/p/5092533.html