Boring count

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

Total Submission(s): 787 Accepted Submission(s): 325

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

官方题解:

1003 Boring count

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

计算以i为结尾的符合条件的最长串两种方法:

1.维护一个起点下标startPos,初始为1。如果当前为i,那么cnt[str[i]]++,如果大于k的话,就while( str[startPos] != str[i+1] ) cnt[str[startPos]]–, startPos++; 每次都保证 startPos~i区间每个字母个数都不超过k个。ans += ( i-startPos+1 )。 时间复杂度O(n)

自己当时怎么没有想到呢?

  1. #include <set>
  2. #include <map>
  3. #include <list>
  4. #include <stack>
  5. #include <cmath>
  6. #include <queue>
  7. #include <string>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <iostream>
  12. #include <algorithm>
  13. #define PI cos(-1.0)
  14. #define RR freopen("input.txt","r",stdin)
  15. using namespace std;
  16. typedef long long LL;
  17. const int Max= 100010;
  18. char str[Max];
  19. int vis[30];
  20. int main()
  21. {
  22. int k;
  23. int T;
  24. int len;
  25. LL sum;
  26. scanf("%d",&T);
  27. while(T--)
  28. {
  29. scanf("%s",str);
  30. scanf("%d",&k);
  31. memset(vis,0,sizeof(vis));
  32. len = strlen(str);
  33. int ans=0;
  34. sum=0;
  35. for(int i=0;i<len;i++)
  36. {
  37. vis[str[i]-'a']++;
  38. while(vis[str[i]-'a']>k)
  39. {
  40. vis[str[ans]-'a']--;
  41. ans++;
  42. }
  43. sum+=(i-ans+1);
  44. }
  45. cout<<sum<<endl;
  46. }
  47. return 0;
  48. }

Boring count(字符串处理)的更多相关文章

  1. hdu Boring count(BestCode round #11)

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

  2. HDU 5056 Boring count(不超过k个字符的子串个数)

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

  3. hdu----(5056)Boring count(贪心)

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

  4. hdu 5056 Boring count

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

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

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

  6. 【HDOJ】P5056 Boring count

    题目意思是给你一个字符串和K,让你求其中有多少个字串中每个字母的出现次数不超过K次,可以等于 题目意思是很简单的,写起来也很简单,不过就是注意最后要是long long要不WA了,555~ #incl ...

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

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

  8. HDU 5056 Boring Count --统计

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

  9. BestCoder11(Div2) 1003 Boring count (hdu 5056) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056 题目意思:给出一条只有小写字母组成的序列S,问当中可以组成多少条每个字母出现的次数 <= ...

随机推荐

  1. CheckBox的用法

    if (window.pageConfig["IsCommend"] == "True") {      $("#IsCommend").v ...

  2. 很赞的PHP字符串加密函数

    最近, 从discuz里面发现了一个很牛的加密解密函数. 此函数的厉害之处在于可以在指定时间内加密还原字符串,超时无法还原 这样我们就可以拿此函数来做很多用途了,比如:单点登录的token加密传输啦, ...

  3. For循环输出九九乘法表

    题:使用For循环输出九九乘法表 解析: 1*1=1 1*2=2  2*2=4 1*3=3  2*3=6  3*3=9 .... 1*9=9  ........ .....9*9=81 可以看做j*i ...

  4. 内存溢出之Tomcat内存配置

    设置Tomcat启动的初始内存其初始空间(即-Xms)是物理内存的1/64,最大空间(-Xmx)是物理内存的1/4. 可以利用JVM提供的-Xmn -Xms -Xmx等选项可进行设置 三.实例,以下给 ...

  5. codeforces343A A. Rational Resistance

    http://http://codeforces.com/problemset/problem/343/A A. Rational Resistance time limit per test 1 s ...

  6. 变形--扭曲 skew()

    变形--扭曲 skew() 扭曲skew()函数能够让元素倾斜显示.它可以将一个对象以其中心位置围绕着X轴和Y轴按照一定的角度倾斜.这与rotate()函数的旋转不同,rotate()函数只是旋转,而 ...

  7. JSon_零基础_005_将po(bean)对象集合List转换为JSon格式的对象字符串,返回给界面

    将po(bean)对象集合List转换为JSon格式的对象字符串,返回给界面 导入jar包: 编写:po(bean)代码: package com.west.webcourse.po; /** * 第 ...

  8. 夺命雷公狗---Thinkphp----1之目录介绍

    ThinkPHP框架 特点: 免费开源 敏捷开发(快速开发) 面向对象 MVC思想 yii,ci之类的框架都有这些特点.是06年到现在的一个老牌框架,现在还是个很不错的框架 可以在thinkphp的官 ...

  9. 机器学习(Machine Learning)&深入学习(Deep Learning)资料

    <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost 到随机森林. ...

  10. OpenGl And 视图

    OpenGl And 视图 标签(空格分隔): game 简介 本文主要介绍坐标系的观念, 以及在openGL中的视图及其相关的变换. 大纲 视图.模型.投影变换概念 Opengl中对各种变换的支持 ...