Boring count

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

Total Submission(s): 360    Accepted Submission(s): 140

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
 

官方题解:

枚举字符串下标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)

2.预处理出全部字母的前缀和。然后通过二分找出以i为结尾的符合条件的最长串的左边界。

时间复杂度O(nlogn),写的不够好的可能超时。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[30];
char s[101000];
int main()
{
int t,k,n;
scanf("%d",&t);
while(t--)
{
memset(s,0,sizeof(s));
memset(a,0,sizeof(a));
scanf("%s",s);
n=strlen(s);
scanf("%d",&k);
long long ans=0;
int start=0;
for(int i=0;i<n;i++)
{
int x=s[i]-'a';
a[x]++;
if(a[x]>k)
{
while(s[start]!=s[i])
{
a[s[start]-'a']--;
start++;
}
a[s[start]-'a']--;
start++;
}
ans+=(i-start+1);
}
printf("%I64d\n",ans);
}
return 0;
}

hdu Boring count(BestCode round #11)的更多相关文章

  1. 设n是奇数,证明:16|(n4+4n2+11)(整除原理1.1.1)

    设n是奇数,证明:16|(n4+4n2+11) 解: 令n=2k+1,k∈z n4+4n2+11 =(2k+1)4+4(2k+1)2+11 =(4k2+4k+1)2+(2k+1)2+11 =16k4+ ...

  2. 【BZOJ1452】[JSOI2009]Count(树状数组)

    [BZOJ1452][JSOI2009]Count(树状数组) 题面 BZOJ 洛谷 题解 数据范围这么小?不是对于每个颜色开一个什么东西记一下就好了吗. 然而我不会二维树状数组? 不存在的,凭借多年 ...

  3. amazeui学习笔记--css(常用组件11)--分页Pagination

    amazeui学习笔记--css(常用组件11)--分页Pagination 一.总结 1.分页使用:还是ul包li的形式: 分页组件,<ul> / <ol> 添加 .am-p ...

  4. 多任务-python实现-协程(2.1.11)

    多任务-python实现-协程(2.1.11) 23/100 发布文章 qq_26624329 @ 目录 1.概念 2.迭代器 1.概念 协程与子例程一样,协程(coroutine)也是一种程序组件. ...

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

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

  6. HDU 5056 Boring count(数学)

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

  7. Linux(10.5-10.11)学习笔记

    3.2程序编码 unix> gcc -01 -o p p1.c p2.c -o用于指定输出(out)文件名. -01,-02 告诉编译器使用第一级或第二级优化 3.2.1机器级代码 机器级编程两 ...

  8. HDU 4557 非诚勿扰(Treap找后继)

    非诚勿扰 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submi ...

  9. HDU 5430 Reflect(欧拉函数)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5430 从镜面材质的圆上一点发出一道光线反射NNN次后首次回到起点. 问本质不同的发射的方案数. 输入描述 ...

随机推荐

  1. 前端构建和模块化工具-coolie

    [前言] 假设你之前用过前端模块化工具:seajs.requirejs. 用过前端构建工具grunt.gulp, 而且感到了一些不方便和痛苦,那么你能够试试coolie [coolie] 本文不是一篇 ...

  2. JavaScript 实例 | w3cschool菜鸟教程

    JavaScript 实例 | w3cschool菜鸟教程 http://www.w3cschool.cc/js/js-examples.html

  3. delphi TOnFormVisibleChangeEvent 事件应用

    TGQIFileMgrForm = class(TForm) 定义 property OnVisibleChange: TOnFormVisibleChangeEvent read FOnVisibl ...

  4. delphi 线程的应用 和spcomm的应用

    http://bbs.csdn.net/topics/390744417 串口控件本身的线程不是这样理解的,你不用管它本身用不用线程,它的内部线程和你也没关系.前面说过了,你可以在自己的主线程里创建好 ...

  5. MyEclipse使用总结——MyEclipse中配置WebLogic12c服务器

    MyEclipse中配置WebLogic12c服务器的步骤如下: [Window]→[Preferences],如下图所示: 找到WebLogic的配置,如下图所示:

  6. 使用Axure RP原型设计实践02,自定义部件以及熟悉与部件相关面板

    本篇体验在Axure中自定义部件,并熟悉Widget Interations and Notes面板,Widget Properties and Style面板,Widget Manager面板. 在 ...

  7. Java 遍历类中的属性

    public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, I ...

  8. nginx网站攻击防护

    1.上上个月架构全部迁移上云以后,总的来说比较稳定,业务量也上来,可爱的坏人也来了,7X24小时不停恶意攻击我的网站,第一次收到报警是网站流入流量1分钟以内连续3次超过1000000bps,换算下1M ...

  9. Windows 同一时候开启核心显卡与独立显卡(不接显示器启动核芯显卡)

    採用Mp4视频压缩格式编码时,非常耗CPU.所以决定上显卡.进行显卡加速.选择了Intel核心显卡进行视频编码加速,效果非常理想.但如今的问题是:在PC上如何同一时候开启核心显卡与独立显卡.经过几番周 ...

  10. Module ngx_http_v2_module

    官方配置说明: http://nginx.org/en/docs/http/ngx_http_v2_module.html#example ngx_http_v2_module模块指令中文说明 ngx ...