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. the difference between an embOS interrupt and a zero latency interrupt

    the difference between an embOS interrupt and a zero latency interrupt is the interrupt priority lev ...

  2. Linux kernel AIO

    http://blog.csdn.net/abcd1f2/article/details/47440087

  3. zoj 1649

    #include <iostream> #include <queue> using namespace std; int n,m,s2,e2; int b[205][205] ...

  4. delphi 处理缩放图像

    procedure TTMEImageDeviceIdentifyFrom.DrawText(AImage : TImage; AFile: string);var I: Integer; iWidt ...

  5. Delph 两个对立程序使用消息进行控制通信

    在实际应用中,总是会遇到两个独立的程序进行通信,其实通信的方式有好几种,比如进程间通信,消息通信. 项目中用到了此功能, 此功能用于锁屏程序, 下面把实现的流程和大家分享一下. 1. 在锁屏程序中,自 ...

  6. concat函數 函數concat 可以用來合拼兩個或以上的字串。

    12. “Mexico 墨西哥”的首都是”Mexico City”. 顯示所有國家名字,其首都是國家名字加上”City”. concat函數 函數concat 可以用來合拼兩個或以上的字串. : SE ...

  7. 【SVN】如果windows用户忘记了svn的用户名和密码怎么办?

    如果windows用户忘记了svn的用户名和密码怎么办? 1>你得进入默认地址 C:\Users\Administrator\AppData\Roaming\Subversion\auth\sv ...

  8. Android基础笔记(九)- 广播

    广播的概念 广播的生命周期 案例-监听短信到来并解析短信内容 案例-拦截外拨电话并设置区号 案例-SD卡状态监听 介绍一些经常使用的广播 发送自己定义广播 有序广播和无序广播 启程!! ! 广播的概念 ...

  9. 为什么用freemarker视图?

    在java领域,表现层技术主要有三种:jsp.freemarker.velocity. jsp是大家最熟悉的技术优点:1.功能强大,可以写java代码2.支持jsp标签(jsp tag)3.支持表达式 ...

  10. Android Activity启动流程源码全解析(2)

    接上之前的分析 ++Android Activity启动流程源码全解析(1)++ 1.正在运行的Activity调用startPausingLocked 一个一个分析,先来看看startPausing ...