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. php对gzip的使用(实例)

    代码如下: if (!function_exists('gzdecode')) { function gzdecode ($data) { $flags = ord(substr($data, 3, ...

  2. mysql数据库要按当天、昨天、前七日、近三十天、季度、年查询

    mysql数据库要按当天.昨天.前七日.近三十天.季度.年查询

  3. 建造者模式(build pattern)-------创造型模式

    将一个复杂对象的构建与它的标示分离,使得同样的构建过程可以创建不同的标示. 建造者模式是较为复杂的创建型模式,它将客户端与包含多个组成部分(或部件)的复杂对象的创建过程分离,客户端无须知道复杂对象的内 ...

  4. VS------修改项目命名空间

    1.以文本形式打开此文件 2.修改一下部分 3.vs会自动提示,选择“放弃”即可

  5. 从头认识java-16.5 nio的数据转换

    这一章节我们来讨论一些nio的数据转换. 上面一章节我们提到ByteBuffer,可是他是面向二进制数据,对于编程来说不是非常方便,因此,java添加了转换数据的工具. 1.asCharBuffer ...

  6. [转载]2014年10月26完美世界校招两道java题

    public class VolitileTest { volatile static int count=0; public static void main(String args[]){ for ...

  7. CentOS 6.3下部署LVS(NAT模式)+keepalived实现高性能高可用负载均衡

    一.简介 VS/NAT原理图: 二.系统环境 实验拓扑: 系统平台:CentOS 6.3 Kernel:2.6.32-279.el6.i686 LVS版本:ipvsadm-1.26 keepalive ...

  8. setTimeOut一些注意的地方

    for (var i = 0; i < data.length; i++) { var flashID = data[i].ID; //setTimeOut(removeFlashDiv(fla ...

  9. IOS深入学习(20)之Object modeling

    1 前言 本节简单的介绍了对象建模,以及需要注意的事项. 2 详述 对象建模是对设计通过一个面向对象应用检测和操作服务的对象或者类的加工.许多模型技术是可能的:Cocoa开发环境不推荐歧义性. 典型地 ...

  10. VIM 插入

    不知道有多少VIM新手和我当年(去年)一样,信誓旦旦的以为只有i可以插入 唉,现在想想都觉得可笑,都是Windows下的编辑器用多了的结果 鼠标一点,妈妈再也不用担心我的文本插入了……悲剧! 好了,让 ...