Boring count

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 828    Accepted Submission(s): 342

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
  1. 3
  2. abc
  3. 1
  4. abcabc
  5. 1
  6. abcabc
  7. 2
 
Sample Output
  1. 6
  2. 15
  3. 21
 
Source
 

题目大意:给出一个字符串,求出子串中每个字母出现次数不超过k的个数。

解题思路:枚举字符串下标i,每次计算以i为结尾的符合条件的最长串。那么以i为结尾的符合条件子串个数就是最长串的长度。求和就可以。

代码例如以下:

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <string>
  9. #include <vector>
  10. #include <deque>
  11. #include <list>
  12. #include <set>
  13. #include <map>
  14. #include <stack>
  15. #include <queue>
  16. #include <numeric>
  17. #include <iomanip>
  18. #include <bitset>
  19. #include <sstream>
  20. #include <fstream>
  21. #include <limits.h>
  22. #define debug "output for debug\n"
  23. #define pi (acos(-1.0))
  24. #define eps (1e-6)
  25. #define inf (1<<28)
  26. #define sqr(x) (x) * (x)
  27. #define mod 1000000007
  28. using namespace std;
  29. typedef long long ll;
  30. typedef unsigned long long ULL;
  31. #define maxn 100010
  32. int n,m,t,k;
  33. char str[maxn];
  34. int num[maxn];
  35. int main()
  36. {
  37. scanf("%d",&t);
  38. while(t--)
  39. {
  40. memset(num,0,sizeof(num));
  41. scanf("%s",str);
  42. scanf("%d",&k);
  43. int k1=strlen(str);
  44. int pos=0;
  45. ll ans=0;
  46. for(int i=0; i<k1; i++)
  47. {
  48. num[str[i]-'a']++;
  49. if(num[str[i]-'a']>k)
  50. {
  51. while(str[pos]!=str[i])
  52. {
  53. num[str[pos]-'a']--;
  54. pos++;
  55. }
  56. num[str[pos]-'a']--;
  57. pos++;
  58. }
  59. ans+=(i-pos+1);
  60. }
  61. printf("%I64d\n",ans);
  62. }
  63. return 0;
  64. }

HDU 5056 Boring count(不超过k个字符的子串个数)的更多相关文章

  1. hdu 5056 Boring count

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

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

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

  3. hdu 5056 Boring count (类似单调队列的做法。。)

    给一个由小写字母构成的字符串S,问有多少个子串满足:在这个子串中每个字母的个数都不超过K. 数据范围: 1<=T<= 1001 <= the length of S <= 10 ...

  4. HDU 5056 Boring count(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056 Problem Description You are given a string S con ...

  5. HDU 5056 Boring Count --统计

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

  6. hdu 5056 所有字母数都<=k的子串数目

    <a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=5056" style="font ...

  7. HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  8. HDU 3518 Boring counting(后缀数组,字符处理)

    题目 参考自:http://blog.sina.com.cn/s/blog_64675f540100k9el.html 题目描述: 找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). ...

  9. hdu Boring count(BestCode round #11)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

随机推荐

  1. 07 mongodb

    mongodb mongodb简介 简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为Web应用提供可扩展的高性能数据存储解决方案.    MongoDB是一个介于关系数据 ...

  2. Cisco路由器交换机基础配置

    交换机配置 交换机基本状态: switch: # ROM状态, 路由器是rommon> hostname> # 用户模式 hostname# # 特权模式 hostname(config) ...

  3. CentOS 7 设置开机自启动

    创建脚本:    #!/bin/bash    echo "hello!" # 启动虚拟环境    cd /data/env/crmenv/bin/    source activ ...

  4. POJ 1949 Chores(DAG上的最长路 , DP)

    题意: 给定n项任务, 每项任务的完成用时t和完成每项任务前需要的k项任务, 求把所有任务完成的最短时间,有当前时间多项任务都可完成, 那么可以同时进行. 分析: 这题关键就是每项任务都会有先决条件, ...

  5. Saving James Bond - Hard Version

    07-图5 Saving James Bond - Hard Version(30 分) This time let us consider the situation in the movie &q ...

  6. keepalived安装脚本

    keepalived_install.sh: #cat keepalived_install.sh #!/bin/bash pkg=keepalived-2.0.10.tar.gz tar xf $p ...

  7. 使用JQuery.slideBox实现图片滚动效果

    1.下载JQuery.slideBox和jquery插件,并引用 <link href="css/jquery.slideBox.css" rel="stylesh ...

  8. xtu summer individual 1 D - Round Numbers

    D - Round Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u D ...

  9. 路线统计(codevs 1482)

    题目描述 Description N个节点的有向图, 求从start到finish刚好经过时间time的总方案数 mod 502630. 输入描述 Input Description 第一行包含一个整 ...

  10. java 字节码 指令集

    有时候为了能理解JVM对程序所做的优化等,需要查看程序的字节码,因此知道了解一些常见的指令集很重要! 指令码 助记符 说明 0x00 nop 什么都不做 0x01 aconst_null 将null推 ...