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. MVC4 Controller 与 WebApi 的 Session 传值问

    在MVC以后,Session方式可能已经不太常用,但偶尔还是会用到,比如页面验证码之类的.例如登录页面使用的验证码通过Controller提供一个View来实现,可以使用Session来存储这个值.但 ...

  2. 利用GetType反射方法再调用方法进行传递参数实现调用

    直接上代码: TestMenuService.MenuServiceCSClient tesClient = new TestMenuService.MenuServiceCSClient(); va ...

  3. IDE、SATA、SCSI、SAS、FC、SSD 硬盘类型

    http://www.cnblogs.com/awpatp/archive/2013/01/29/2881431.html

  4. HTTP 错误 404.3 解决

    问题 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 解决 在开始菜单中找到 Visual Studio 命令提示工具.然后用管理员 ...

  5. java中被遗忘的native关键字

    我是无意间看见JNI( java调用动态链接库dll )这块的东西. 所有记下来:本地声明方法  装载完成dll文件后,将使用的方法用native关键字声明. public native static ...

  6. linux连接sybase数据库-isql

    转自:http://blog.knowsky.com/196438.htm 想要linux连接sybase数据库用命令isql: isql [-U login id] [-P password] [- ...

  7. 如何连接 MySQL

    操作如下: [root@localhost ~]$ mysql -uroot -p' # 本地连接 MySQL 服务 [root@localhost ~]$ mysql -uroot -p' -h19 ...

  8. Serlvet学习笔记之二—不同页面共享数据

    一共有四种方法实现共享页面共享数据 1.cookie 2.sendRedirect 3.session 4.隐藏表单提交(form) 5.ServletContex 1.cookie:服务器在客户端保 ...

  9. m2014-android->安卓开发在线案例

    1.www.javaapk.com account: 克里斯  pwd: xiaobai 这个网站非常不错,各种例子,各种可供下载演示的项目,用了就知道,好处不多说,真心赞!

  10. Android项目结构介绍

    src/存放Java源代码gen/中存放系统自动生成的配置文件Android 4.4.2下包含android.jar文件,这是一个Java归档文件,其中包含构建应用程序所需的所有的Android SD ...