Boring count

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 228    Accepted Submission(s): 90

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
 
Source
 
   之前用一种动态规划式方法怎么用怎么TLE ,看了一下解题报告,发现有O(n)算法。觉得很是强大,........哎,还是太弱哇...╮(╯▽╰)╭
代码:

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=;
char str[maxn];
int sac[];
int main()
{
int cas,st,pos,k;
scanf("%d",&cas);
while(cas--)
{
memset(sac,,sizeof(sac));
scanf("%s %d",str,&k);
__int64 ans=;
st=;
for(int i=;str[i]!='\0';i++)
{
pos=str[i]-'a';
sac[pos]++;
if(sac[pos]>k)
{
while(str[st]!=str[i])
{
sac[str[st]-'a']--;
st++;
}
sac[str[st]-'a']--;
st++;
}
ans+=(i-st+);
}
printf("%I64d\n",ans);
}
return ;
}

hdu----(5056)Boring count(贪心)的更多相关文章

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

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

  2. hdu 5056 Boring count

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

  3. HDU 5056 Boring count(数学)

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

  4. HDU 5056 Boring Count --统计

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

  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. hdu 5056 Boring count (类似单调队列的做法。。)

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

  7. hdu Boring count(BestCode round #11)

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

  8. Boring count(字符串处理)

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

  9. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

随机推荐

  1. ProgressDialog 的 使用

    一 . ProgressDialog ProgressDialog是AlertDialog类的一个扩展,可以为一个未定义进度的任务显示一个旋转轮形状的进度动画,或者为一个指定进度的任务显示一个进度条. ...

  2. HDU 4442 Physical Examination

    Physical Examination Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  3. 如何选择正确的DevOps工具

    坦白的讲:世界上没有哪种工具能够像DevOps这么神奇(或敏捷,或精益).DevOps在开发和运营团队之间建立了完美的合作与沟通,因此与其说这是一种神奇的工具,不如说是一种文化的转变. 然而,团队之间 ...

  4. [SAP ABAP开发技术总结]BAPI调用

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. eclipse 实用快捷键

    Ctrl+E: 弹出输入窗口,可快速返回想返回的目标界面 Ctrl+Shift+O: 快速import包 Ctrl+O:显示类中方法和属性的大纲 Ctrl+1 快速修复 Ctrl+Alt+↓ 复制当前 ...

  6. 检测服务器IP端口是否可用

    private static double Scanner(string ip, int port) { try { System.Net.Sockets.TcpClient Tcp = new Sy ...

  7. 对称加密DES和TripleDES

    一.  对称加密 对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码).因此,通信双方都必 ...

  8. 表单美化-原生javascript和jQuery下拉列表(兼容IE6)

    效果: 思想:用其他标签配合脚本和隐藏的input并通过传值模拟表单元素中的select <!DOCTYPE HTML> <html lang="en-US"&g ...

  9. iOS - OC NSNull 空值

    前言 @interface NSNull : NSObject <NSCopying, NSSecureCoding> 作为占据空间的一个空值,如用在数组或字典中占据一个没有任何值的空间. ...

  10. html一般标签、常用标签、表格

    body的属性: bgcolor               页面背景色 text                    文字颜色 topmargin            上边距 leftmargi ...