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. Cheatsheet: 2015 01.01~ 01.31

    JAVA JVM Architecture Improving Lock Performance in Java 10 Best Java Tools That Every Java Programm ...

  2. Java_你应该知道的26种设计模式

    四. 模板方法模式 Definition: Define the skeleton of an algorithm in an operation, deferring some steps to s ...

  3. HashCheck

    https://github.com/gurnec/HashCheck

  4. 字符串表达式String Expressions

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

  5. ruby的正则表达式-scan方法

    irb(main):001:0> str_vps=%Q{viewpoint_ids: [{"id":"260e053b-d728-4785-888d-eb4f1ca ...

  6. 一个CSS中Z-index的用法

    一个CSS中Z-index的用法 CSS教程:彻底掌握Z-index属性     大多数的CSS属性都很容易使用.常常,当您对标记语言的元素使用CSS属性时,产生的结果会随着您刷新页面而立即呈现.而另 ...

  7. 上不了Google是码农的悲哀

    http://refyt.com/?r=34d1edb7dba42e8d 上不了Google是码农的悲哀.1. 资料大部分都在国外的网站,差不多倍感伤心.2. Google Play没有办法访问了.3 ...

  8. 手机如何解散QQ讨论组

    手机如何解散QQ讨论组   讨论组可以方便一群人的聊天,一般都是一段时间的问题.过了这一段时间,大家都是不需要再在讨论组里面发言了,那么手机如何解散QQ讨论组呢?     1 我们登录自己的 QQ之后 ...

  9. 机器学习十大算法之KNN(K最近邻,k-NearestNeighbor)算法

    机器学习十大算法之KNN算法 前段时间一直在搞tkinter,机器学习荒废了一阵子.如今想重新写一个,发现遇到不少问题,不过最终还是解决了.希望与大家共同进步. 闲话少说,进入正题. KNN算法也称最 ...

  10. nodejs学习笔记<六>文件处理

    nodejs处理文件模块:fs  —>  var fs = require(‘fs’); 读取文件:readFileSync & readFile 读取文件路径为绝对: 读取结果需要to ...