Boring count

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

Total Submission(s): 828    Accepted Submission(s): 342

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
 

题目大意:给出一个字符串,求出子串中每个字母出现次数不超过k的个数。

解题思路:枚举字符串下标i,每次计算以i为结尾的符合条件的最长串。那么以i为结尾的符合条件子串个数就是最长串的长度。求和就可以。

代码例如以下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
#define maxn 100010
int n,m,t,k;
char str[maxn];
int num[maxn];
int main()
{
scanf("%d",&t);
while(t--)
{
memset(num,0,sizeof(num));
scanf("%s",str);
scanf("%d",&k);
int k1=strlen(str);
int pos=0;
ll ans=0;
for(int i=0; i<k1; i++)
{
num[str[i]-'a']++;
if(num[str[i]-'a']>k)
{
while(str[pos]!=str[i])
{
num[str[pos]-'a']--;
pos++;
}
num[str[pos]-'a']--;
pos++;
}
ans+=(i-pos+1);
}
printf("%I64d\n",ans);
}
return 0;
}

HDU 5056 Boring count(不超过k个字符的子串个数)的更多相关文章

  1. hdu 5056 Boring count

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

  2. hdu 5056 Boring count (窗体滑动)

    You are given a string S consisting of lowercase letters, and your task is counting the number of su ...

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

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

  4. HDU 5056 Boring count(数学)

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

  5. HDU 5056 Boring Count --统计

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

  6. hdu 5056 所有字母数都<=k的子串数目

    <a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=5056" style="font ...

  7. HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  8. HDU 3518 Boring counting(后缀数组,字符处理)

    题目 参考自:http://blog.sina.com.cn/s/blog_64675f540100k9el.html 题目描述: 找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). ...

  9. hdu Boring count(BestCode round #11)

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

随机推荐

  1. C#语言之字符串和正则表达式

    本文将完成以下两个目标: 一.创建字符串: 二.正则表达式: 首先,我先来介绍一下System.String类: System.String是一个类,专门用于存储字符串,允许对字符串进行许多操作. 使 ...

  2. 【BZOJ 1076】[SCOI2008]奖励关(期望)

    Description 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物, 每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的 ...

  3. p3386 二分图匹配模板

    https://www.luogu.org/problemnew/show/P3386 可以只做一边的匹配 #include <bits/stdc++.h> using namespace ...

  4. php file_get_contents json_decode 输出为NULL

    解决办法一:不小心在返回的json字符串中返回了BOM头的不可见字符,某些编辑器默认会加上BOM头,如下处理才能正确解析json数据: $info = json_decode(trim($info,c ...

  5. [SQL]数据库中对值为数字,存储格式为varchar类型的字段进行排序

    如果要对数据库中某存储数字的列(存储类型不为int)进行排序,可以在order by 里对该列进行转换, 即如 order by cast(mycolumn as int) desc

  6. ASP.NET MVC WebAPI请求

    基础: 首先,先创建一个“ASP.NET 空Web应用程序” : 然后添加对 “System.Web.Http” 和 “System.Web.Http.WebHost” 的引用: 再添加对“Syste ...

  7. BC#76.2DZY Loves Balls

    DZY Loves Balls  Accepts: 659  Submissions: 1393  Time Limit: 4000/2000 MS (Java/Others)  Memory Lim ...

  8. [luoguP1027] Car的旅行路线(Floyd)

    传送门 建图麻烦,建完图搞一遍Floyd就好了. ——代码 #include <iostream> #include <cstdio> #include <cmath&g ...

  9. 【思维】2017多校训练七 HDU6121 Build a tree

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 [题意] 询问n个结点的完全k叉树,所有子树结点个数的异或和是多少 [思路] 一棵完全K叉树,对于树的每一 ...

  10. bzoj 2463 [中山市选2009]谁能赢呢? 博弈

    [中山市选2009]谁能赢呢? Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3014  Solved: 2165[Submit][Status][D ...