题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056

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
 
Source


官方题解:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjg2MDA2Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">


代码例如以下:
#include <cstdio>
#include <cstring>
const int maxn = 100017;
int main()
{
int t;
char str[maxn];
int st[27];
int k;
scanf("%d",&t);
while(t--)
{
memset(st,0,sizeof(st));
scanf("%s",str);
scanf("%d",&k);
__int64 ans = 0;
int len = strlen(str);
int startpos = 0;
for(int i = 0; i < len; i++)
{
int tt = str[i] -'a';
st[tt]++;
if(st[tt] > k)
{
while(str[startpos]!=str[i])
{
st[str[startpos]-'a']--;
startpos++;
}
st[str[startpos]-'a']--;
startpos++;
}
ans+=i-startpos+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

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

  3. HDU 5056 Boring Count --统计

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

  4. hdu 5056 Boring count (窗体滑动)

    You are given a string S consisting of lowercase letters, and your task is counting the number of su ...

  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. [AGC008F] Black Radius(树形dp)

    神题啊!! Description 给你一棵有N个节点的树,节点编号为1到N,所有边的长度都为1 "全"对某些节点情有独钟,这些他喜欢的节点的信息会以一个长度为N的字符串s的形式给 ...

  2. BZOJ【1639】: [Usaco2007 Mar]Monthly Expense 月度开支

    1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 700  Solved: ...

  3. Replacing Accented characters(Diacritic) .NET

    原文发布时间为:2012-02-17 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Collections.Generic;using Syste ...

  4. 生成静态页面方法 .NET

    原文发布时间为:2009-09-30 -- 来源于本人的百度文章 [由搬家工具导入] 采用模板法:【例子中的两个页面以及生成的页面均在同一个目录,自己可以去改】 模板Template.htm: < ...

  5. datalist、repearter、gridview显示行号的三种方法 或者是获取datalist行id

    原文发布时间为:2009-05-06 -- 来源于本人的百度文章 [由搬家工具导入] 1. if you are using SQL Server, try select identity(int,1 ...

  6. 各种版本QT下载地址与VS2013+QT5.3.1环境搭建过程(转)

    原文转自 http://blog.csdn.net/baidu_34678439/article/details/54586058 1. 所有Qt版本下载地址: http://download.qt. ...

  7. javascript 动态添加城市

    匿名函数的使用 createTextnode 创建文本 createElement   创建元素 appendChild     将文本或元素追加 <!DOCTYPE html> < ...

  8. 横竖屏切换,activity重建问题

    最近有个需求,横屏直播A退出后返回直播列表页B(竖屏)时,在小米8上列表页B直接变成横屏的了,因为列表页B由竖屏切换成横屏了,还会重新执行生命周期onCreate()-onResume()等等. 为了 ...

  9. 牛客网 牛客小白月赛2 A.数字方阵-反魔方阵,梁邱构造法

    天坑未补... 水一波博客,再不写博客就咸成鱼干了,只写题不写题解,过一段时间就忘了自己学过什么了. 最近重点就是把开学以来写的题补出来,没学的就滚去学会啊(= =),填一下坑... 从这篇博客开始, ...

  10. Gym 101064 D Black Hills golden jewels 【二分套二分/给定一个序列,从序列中任意取两个数形成一个和,两个数不可相同,要求求出第k小的组合】

    D. Black Hills golden jewels time limit per test 2 seconds memory limit per test 256 megabytes input ...