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

题目大意:求出一个字符串的子串中反复的小写字母不超过K个的个数。

思路:使用窗体滑动的方法,设置两个指针,分别指在串的左边和右边。当不满足条件时左指针向右移动,直到将当前的右指针删除到符合条件

。然后从原来不符合条件的下一个字母再開始计数。


#include <iostream>

#include <algorithm>

#include <cstdio>

#include <cstring>

#define LL __int64

#define inf 0x3f3f3f3f

char s[100010];

LL sum[100010];

using namespace std;

int main()

{

    LL n,m,cla,i,j,l;

    scanf("%I64d",&cla);

    while(cla--)

    {

        memset(sum,0,sizeof(sum));

        scanf("%s %I64d",s,&n);

        l=strlen(s);

        LL pos=0,ans=0;

        for(i=0;i<l;i++)

        {

            sum[s[i]-'a']++;

            while(sum[s[i]-'a']>n)//当遇到不符合条件的时候左指针右移,并删除经过的字母的累计次数

            {

                sum[s[pos]-'a']--;

                pos++;

            }

            ans+=i-pos+1;//当符合条件的时候就加上当前这段的字母个数

        }

        printf("%I64d\n",ans);

    }

    return 0;

}

hdu 5056 Boring count (窗体滑动)的更多相关文章

  1. HDU 5056 Boring count(不超过k个字符的子串个数)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  2. HDU 5056 Boring count(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056 Problem Description You are given a string S con ...

  3. hdu 5056 Boring count

    贪心算法.需要计算分别以每个字母结尾的且每个字母出现的次数不超过k的字符串,我们设定一个初始位置s,然后用游标i从头到尾遍历字符串,使用map记录期间各个字母出现的次数,如果以s开头i结尾的字符串满足 ...

  4. HDU 5056 Boring Count --统计

    题解见官方题解,我这里只实现一下,其实官方题解好像有一点问题诶,比如 while( str[startPos] != str[i+1] ) cnt[str[startPos]]--, startPos ...

  5. hdu 5056 Boring count (类似单调队列的做法。。)

    给一个由小写字母构成的字符串S,问有多少个子串满足:在这个子串中每个字母的个数都不超过K. 数据范围: 1<=T<= 1001 <= the length of S <= 10 ...

  6. hdu Boring count(BestCode round #11)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  7. Boring count(字符串处理)

    Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. hdu----(5056)Boring count(贪心)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. hdu 4961 Boring Sum(高效)

    pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...

随机推荐

  1. 基于.Net实现前端对话框和消息框

    关于前端对话框.消息框的优秀插件多不胜数.造轮子是为了更好的使用轮子,并不是说自己造的轮子肯定好.所以,这个博客系统基本上都是自己实现的,包括日志记录.响应式布局等等一些本可以使用插件的.好了,废话不 ...

  2. mybatis由浅入深day01_ 7输入映射(7.1传递pojo的包装对象_7.2#{}与${}_7.3传递简单类型_7.4传递pojo对象_7.5传递hashmap)

    7 输入映射 通过parameterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类型. 7.1 传递pojo的包装对象 7.1.1 需求 完成用户信息的综合查询,需要 ...

  3. Maven(一)-- 基础知识

    一.Maven的基本概念 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.项目 ...

  4. docker search 详解

    docker search 用于从 Docker Hub(https://hub.docker.com)中搜索指定的镜像,用法如下: [root@localhost ~]$ docker search ...

  5. 关于.NET玩爬虫这些事

    这几天在微信群里又聊到.NET可以救中国但是案例太少不深的问题,我说.NET玩爬虫简直就是宇宙第一,于是大神朱永光说,你为何不来写一篇总结一下? 那么今天就全面的来总结一下,在.NET生态下,如何玩爬 ...

  6. iOS富文本组件的实现—DTCoreText源码解析 数据篇

    本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需 ...

  7. linux 个性化设置shell提示

    1.linux 用户登录过程中 相关文件执行顺序: /etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~/.bashrc → [/etc/b ...

  8. DOS cmd - how to ping a remote host with specified port

    You can use ping to test whether you can connect to a remote host: ping baidu.com ping 125.6.45.88 ( ...

  9. 点击button,button背景图片变化

    1.设置背景渐变效果,在drawable目录下建buttonshape.xml文件, 内容为: <?xml version="1.0" encoding="utf- ...

  10. crossdomain.xml跨域配置文件的安全注意事项

    零.绪论: 对WEB中的FLASH确实了解不多,对程序中的跨域配置也了解不多,这是自己以前写的一篇笔记,到现在也还了解不深,勉强记下来罢了,备忘. 一.什么是crossdomain.xml?这是一个f ...