1. <a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=5056" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">http://acm.hdu.edu.cn/showproblem.php?pid=5056</a>

所有字母个数都不超过k的字串数目

比赛时候用模拟+组合数学过的,是O(2*26*N)的复杂度,但是没有正解快

遍历每个恰好符合条件的[i,j],其中若包含[i,jj]其中jj是上次计数的最远的j,就+一次i~j -一次i~jj

过的比较险

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <string>
  6. #include <queue>
  7. #include <vector>
  8. #include<set>
  9. #include <iostream>
  10. #include <algorithm>
  11. using namespace std;
  12. #define RD(x) scanf("%d",&x)
  13. #define RD2(x,y) scanf("%d%d",&x,&y)
  14. #define clr0(x) memset(x,0,sizeof(x))
  15. typedef long long LL;
  16. LL gcd(LL a,LL b){ return b == 0? a:gcd(b,a%b);}
  17. char s[100005];
  18. int n,k;
  19. LL ans;
  20. int cnt[26][100005];
  21. void work()
  22. {
  23. //clr0(cnt);
  24. scanf("%s",s);
  25. RD(k);
  26. n = strlen(s);
  27. for(int i = 0;i < 26;++i)
  28. memset(cnt[i],0,sizeof(int)*(n+1));
  29. ans = 0;
  30. for(int i = 1;i <= n;++i){
  31. for(int l = 0;l < 26;++l){
  32. cnt[l][i] = cnt[l][i-1];
  33. }
  34. cnt[s[i - 1] - 'a'][i]++;
  35. }
  36. int mx;
  37. for(int i = 0,j = 1,jj = 0;i <= n;++i){
  38. while(j <= n){
  39. mx = 0;
  40. for(int l = 0;l < 26;++l){
  41. mx = max(mx,cnt[l][j] - cnt[l][i]);
  42. }
  43. if(mx > k)
  44. break;
  45. ++j;
  46. }
  47. if(j == jj)
  48. continue;
  49. if(mx > k){
  50. ans += 1LL*(j-i)*(j-i-1)/2;
  51. if(jj > i)
  52. ans -= 1LL*(jj-i)*(jj-i-1)/2;
  53. jj = j;
  54. //cout<<ans<<endl;
  55. }
  56. else if(j > n){
  57. ans += 1LL*(n-i)*(n-i+1)/2;
  58. if(jj > i)
  59. ans -= 1LL*(jj-i)*(jj-i-1)/2;
  60. break;
  61. }
  62. }
  63. printf("%I64d\n",ans);
  64. }
  65. int main() {
  66. int _;
  67. RD(_);
  68. while(_--){
  69. work();
  70. }
  71. return 0;
  72. }

正解果然要快很多

  1. 枚举字符串下标i,每次计算以i为结尾的符合条件的最长串。那么以i为结尾的符合条件子串个数就是最长串的长度。求和即可。
  2. 计算以i为结尾的符合条件的最长串两种方法:
  3. 1.维护一个起点下标startPos,初始为1。如果当前为i,那么cnt[str[i]]++,如果大于k的话,就while( str[startPos] != str[i] ) cnt[str[startPos]]--, startPos++; 每次都保证 startPos~i区间每个字母个数都不超过k个。ans += ( i-startPos+1 )。 时间复杂度O(n)
  4. 2.预处理出所有字母的前缀和。然后通过二分找出以i为结尾的符合条件的最长串的左边界。时间复杂度O(nlogn),写的不够好的可能超时。
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <string>
  6. #include <queue>
  7. #include <vector>
  8. #include<map>
  9. #include <iostream>
  10. #include <algorithm>
  11. using namespace std;
  12. #define RD(x) scanf("%d",&x)
  13. #define RD2(x,y) scanf("%d%d",&x,&y)
  14. #define clr0(x) memset(x,0,sizeof(x))
  15. typedef long long LL;
  16. char s[100005];
  17. int n,k;
  18. LL ans;
  19. int cnt[256];
  20. void work()
  21. {
  22.     scanf("%s",s+1);
  23.     RD(k);
  24.     n = strlen(s+1);
  25.     clr0(cnt);
  26.     ans = 0;
  27.     int st = 1;
  28.     for(int i = 1;i <= n;++i){
  29.         cnt[s[i]]++;
  30.         if(cnt[s[i]] > k){
  31.             while(1){
  32.                 cnt[s[st]]--,st++;
  33.                 if(s[st-1] == s[i])
  34.                     break;
  35.             }
  36.         }
  37.         ans += i - st + 1;
  38.     }
  39.     printf("%I64d\n",ans);
  40. }
  41. int main() {
  42.     int _;
  43.     RD(_);
  44.     while(_--){
  45.         work();
  46.     }
  47.     return 0;
  48. }
  1.  

hdu 5056 所有字母数都<=k的子串数目的更多相关文章

  1. HDU 1565 - 方格取数(1) - [状压DP][网络流 - 最大点权独立集和最小点权覆盖集]

    题目链接:https://cn.vjudge.net/problem/HDU-1565 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32 ...

  2. 《程序员代码面试指南》第七章 位运算 在其他数都出现k 次的数组中找到只出现一次的数

    题目 在其他数都出现k 次的数组中找到只出现一次的数 java 代码 package com.lizhouwei.chapter7; /** * @Description: 在其他数都出现k 次的数组 ...

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

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

  4. 用js编程输出100以内所有的质数和个数(提示:一个大于1的自然数,除了1和它本身外,不能被其他自然数整除的数都是质数)

    <script type="text/javascript"> for(var i = 3; i <= 100; i ++) {//控制2-100所有的数i fo ...

  5. 75 int类型数组中除了一个数出现一次或两次以外,其他数都出现三次,求这个数。[2行核心代码]

    [本文链接] http://www.cnblogs.com/hellogiser/p/single-number-of-array-with-other-three-times.html [题目] i ...

  6. 网络流(最大流) HDU 1565 方格取数(1) HDU 1569 方格取数(2)

      HDU 1565 方格取数(1) 给你一个n*n的格子的棋盘,每个格子里面有一个非负数.从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的 ...

  7. 一个int 数组,里面数据无任何限制,要求求出所有这样的数a[i],其左边的数都小于等于它,右边的数都大于等于它。能否只用一个额外数组和少量其它空间实现。

    一个int数组, 比如 array[],里面数据无任何限制,要求求出 所有这样的数array[i],其左边的数都小于等于它,右边的数都大于等于它.能否只用一个额外数组和少量其它空间实现. 分析:这题很 ...

  8. JS 实现计算一段文字中的字节数,字母数,数字数,行数,汉字数。

    看到了匹配,第一个想到了用正则表达式,哈哈,果然很方便.不过正则表达式高深莫测!我还没有研究明白啊..目前学了点皮毛.代码如下: <!DOCTYPE html PUBLIC "-//W ...

  9. 给自己名字abel.这个好,怎么字母排序都第一

    给自己名字abel.这个好,怎么字母排序都第一

随机推荐

  1. Cmder安装配置

    转: 1)Windows 命令行增强 cmder chocolatey 配置指南 2) Windows必备神器Cmder使用教程 3)Windows上的程序员神器——Cmder 4)Windows命令 ...

  2. 阿里巴巴Java开发手册及Java代码规约扫描eclipse插件

    一.github地址: https://github.com/alibaba/p3c 二..eclipse插件的安装 此处示例采用eclipse,版本为 Neon.1 Release RC3 (4.6 ...

  3. python爬虫之urlError异常处理

    1.URLError URLError产生的原因: (1)网络无连接,即本机无法上网 (2)连接不到特定的服务器 (3)服务器不存在 import urllib.request import urll ...

  4. MySQL 索引 INDEX

    索引用于快速找出在某列中有特定值的行. 不使用索引,MySQL必须从第一条记录开始读完整个表,直到找到相关的行,表越大,查询数据所花费的时间就越多,如果表中查询的列有一个索引,MySQL能够快速到达一 ...

  5. eclipse打开出现Failed to create the java virtual machine

    低配伤不起呀... 这个问题经常是由于创建JAVA 虚拟机时,内存不足导致的,怎么办呢? 1.找到这么个文件:eclipse.ini(在哪?与ECLIPSE.EXE在一起,在一起...) 2.修改里面 ...

  6. jdk8 tomcat7

    今天想使用jdk8+tomcat7来个写程序,但是一运行始终提示连接不到数据库 提示:Could not obtain connection metadata org.apache.tomcat.db ...

  7. BP神经网络测试MNIST记录

    约定: 所有的初始化权值范围,如下,就是说更换激活函数的情况,没有过大的调整初始权重. if(randomMode==1): numpy.random.seed(seedWih) self.wih = ...

  8. linux安装dpkg安装缺少依赖项的解决

    问题: dpkg: error processing package rxvt:i386 (--install): dependency problems - leaving unconfigured ...

  9. MVCS框架的注意点

    1.Service最好用一个接口对应一个service(便于增加扩展方法) 2.除Services和Model外都需继承自各自对应的父类 3.View不要轻易重写Start和Awake方法(含与启动有 ...

  10. vue2.0 移动端,下拉刷新,上拉加载更多插件 转:

    转自:http://www.cnblogs.com/gmajip/p/7204072.html <template lang="html"> <div class ...