hdu5056 Boring count (尺取)

Problem Description

You 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.

Input

In 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

Output

For each case, output a line contains the answer.

Sample Input

3
abc
1
abcabc
1
abcabc
2

Sample Output

6
15
21

分析:

枚举左端点尺取

ps:
简单尺取
很快写完结果细节没处理好过不去样例,一度怀疑自己写错了
浪费好多时间找bug
码一下细节防止再犯

code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
#define int long long
const int maxm=1e5+5;
char s[maxm];
int mark[26];
int n,k;
void init(){
    memset(mark,0,sizeof mark);
}
signed main(){
    int T;
    scanf("%lld",&T);
    while(T--){
        init();
        scanf("%s%lld",s+1,&k);
        n=strlen(s+1);
        int r=1;
        int sum=0;
        int ans=0;
        for(int i=1;i<=n;i++){//枚举左端点
            while(r<=n&&sum==0){//找到不满足条件的右边界
                int t=s[r++]-'a';
                mark[t]++;
                if(mark[t]==k+1){//超过k
                    sum++;
                }
            }
            if(sum){//如果不满足,则右端点不能加进去
                ans+=(r-1-1)-i+1;//r要减1才是当前的右端点,找了好久才找到这个地方错了
            }else{//如果满足,则右端点可以加进去
                ans+=(r-1)-i+1;
            }
            int t=s[i]-'a';//左端点移动一格
            mark[t]--;
            if(mark[t]==k){//不超过k
                sum--;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值