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. c++ _int64 转成string

    _i64toa(a,buffer,10); scanf("%I64d",&a);printf("%I64d",a); 就可以正确输入输出了.当使用uns ...

  2. Oracle过程及函数的参数模式详解

    一.In.out.in out模式 在Oracle中过程与函数都可以有参数,参数的类型可以指定为in.out.in out三种模式. 三种参数的具体说明,如下图所示: (1)in模式 in模式是引用传 ...

  3. jQuery实现HTML表格单元格的合并功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. SQLServer------如何快速插入几万条测试数据

    方法一: 1.建表 if OBJECT_ID('test') is not null drop table test go create table test (id ,),vid ), constr ...

  5. Java精选笔记_网络编程

    网络编程 概述 现在的网络编程基本上都是基于请求/响应方式的,也就是一个设备发送请求数据给另外一个,然后接收另一个设备的反馈. 在网络编程中,发起连接程序,也就是发送第一次请求的程序,被称作客户端(C ...

  6. python2.0 s12 day7

    开发的第二阶段 网络编程阶段 之所以叫网络编程,是因为,这里面就不是你在一台机器中玩了.多台机器,CS架构.即客户端和服务器端通过网络进行通信的编程了. 首先想实现网络的通信,你得先学网络通信的一个基 ...

  7. 查看网卡流量:sar

    sar(System Activity Reporter 系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,但我们一般用来监控网卡流量 [roo ...

  8. 简要说说NUC972和linux的那些大坑

    刚开始装虚拟机,按照步骤,一步一步,装完,发现虚拟机连不上网,后来在网上得知得需要启动虚拟机设置,可是观察我的虚拟机并没有该选项,起初我认为是版本的问题,可是后来才发现,一时贪便宜,图省事,就没有注册 ...

  9. 你知道嵌入式C语言中各变量存储的位置吗?

    局部变量.局部静态变量.全局变量.全局静态变量区别如下: 局部变量: 栈区: 局部静态变量:静态区: 全局变量: 静态区的常量区: 全局静态变量:静态区. 在进行C/C++编程时,需要程序员对内存的了 ...

  10. beginUpdates和endUpdates-实现UITableView的动画块

    我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作. 这样,我们就会用到 [tableView beginUpdates]; if (newCount ...