题目链接: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. 洛谷 P1783 海滩防御

    题目描述 WLP同学最近迷上了一款网络联机对战游戏(终于知道为毛JOHNKRAM每天刷洛谷效率那么低了),但是他却为了这个游戏很苦恼,因为他在海边的造船厂和仓库总是被敌方派人偷袭.于是,WLP动用了他 ...

  2. TCP/IP协议详解笔记——ARP协议和RARP协议

    ARP:地址解析协议 对于以太网,数据链路层上是根据48bit的以太网地址来确定目的接口,设备驱动程序从不检查IP数据报中的目的IP地址.ARP协议为IP地址到对应的硬件地址之间提供动态映射. 工作过 ...

  3. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---21

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  4. pyqt线程实现

    # coding=utf-8 __author__ = 'a359680405' from PyQt5.QtCore import * from PyQt5.QtGui import * from P ...

  5. python第三方库安装和卸载

    库的安装与卸载 pip install requests pip uninstall requests 查看安装好的库 pip list   第三方库的各种安装方式如下: 一.包管理器 Python有 ...

  6. HDU5006 Resistance(高斯消元)

    给你一个复杂的网路图,然后告诉你s,t,求s,t的等效电阻.方法是设s的电势为1,t的电势为0.然后对于其它的每个点x,满足的是sigma(ux-uy)/R(x,y)(即对每个与x相连的节点y,电势差 ...

  7. LeetCode OJ-- Reorder List **

    https://oj.leetcode.com/problems/reorder-list/ 将一个链表重新排序,比如 1 2 3 4 5,变成 1 5 2 4 3 1.找到中间节点 mid 2.将链 ...

  8. Fresco使用之OOM问题记录

    最近友盟上5.0以上系统报出很多OOM异常,看下日志看到facebook的时候就知道一定是Fresco使用不当导致了OOM. java.lang.OutOfMemoryError: Failed to ...

  9. TopCoder SRM 660 Div2 Problem 1000 Powerit (积性函数)

    令$f(x) = x^{2^{k}-1}$,我们可以在$O(k)$的时间内求出$f(x)$. 如果对$1$到$n$都跑一遍这个求解过程,时间复杂度$O(kn)$,在规定时间内无法通过. 所以需要优化. ...

  10. Codeforces 691E Xor-sequences(矩阵加速DP)

    题目链接 Xor-sequences 利用矩阵加速. 先预处理出当序列长度为$2$的时候的方案数. 也就是说这个序列起点是$a[i]$终点是$a[j]$且中间没有任何元素. 但是所求的$k$很大,序列 ...