HDU 5056 Boring count(不超过k个字符的子串个数)
Boring count
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 828 Accepted Submission(s): 342
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
3
abc
1
abcabc
1
abcabc
2
6
15
21
题目大意:给出一个字符串,求出子串中每个字母出现次数不超过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个字符的子串个数)的更多相关文章
- hdu 5056 Boring count
贪心算法.需要计算分别以每个字母结尾的且每个字母出现的次数不超过k的字符串,我们设定一个初始位置s,然后用游标i从头到尾遍历字符串,使用map记录期间各个字母出现的次数,如果以s开头i结尾的字符串满足 ...
- hdu 5056 Boring count (窗体滑动)
You are given a string S consisting of lowercase letters, and your task is counting the number of su ...
- hdu 5056 Boring count (类似单调队列的做法。。)
给一个由小写字母构成的字符串S,问有多少个子串满足:在这个子串中每个字母的个数都不超过K. 数据范围: 1<=T<= 1001 <= the length of S <= 10 ...
- HDU 5056 Boring count(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056 Problem Description You are given a string S con ...
- HDU 5056 Boring Count --统计
题解见官方题解,我这里只实现一下,其实官方题解好像有一点问题诶,比如 while( str[startPos] != str[i+1] ) cnt[str[startPos]]--, startPos ...
- hdu 5056 所有字母数都<=k的子串数目
<a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=5056" style="font ...
- HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- HDU 3518 Boring counting(后缀数组,字符处理)
题目 参考自:http://blog.sina.com.cn/s/blog_64675f540100k9el.html 题目描述: 找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). ...
- hdu Boring count(BestCode round #11)
Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- Python Tornado简单的http request
这是关于chunk encoding传输以前相关传输编码的处理.没有做压缩解码的处理. import tornado.ioloop import tornado.iostream import soc ...
- Leetcode 224.基本计算器
基本计算器 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式可以包含左括号 ( ,右括号 ),加号 + ,减号 -,非负整数和空格 . 示例 1: 输入: "1 + 1 ...
- jmesa应用
一直以来,都没发现什么好的分页组件,最初时用过displaytag,仔细研究了一下,发现它并没有别人说的那么强大,至少离自己的期望还很远,因此尝试寻找其它新的分页组件,但很久以来都没发现自己满意的.无 ...
- PHP目录文件遍历
<meta charset="utf-8"><?php //遍历文件中的所有文件名称 foreach(glob("phpmyadmin/*") ...
- 最长递增子序列 (LIS) Longest Increasing Subsequence
问题描述: 有一个长为n的数列a0, a1,..., an-1.请求出这个序列中最长的上升子序列.请求出这个序列中最长的上升子序列. 上升子序列:对于任意i<j都满足ai<aj的子序列. ...
- [NOIP2003] 提高组 洛谷P1039 侦探推理
题目描述 明明同学最近迷上了侦探漫画<柯南>并沉醉于推理游戏之中,于是他召集了一群同学玩推理游戏.游戏的内容是这样的,明明的同学们先商量好由其中的一个人充当罪犯(在明明不知情的情况下),明 ...
- BZOJ3408: [Usaco2009 Oct]Heat Wave 热浪
最短路模板.选迪杰. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<alg ...
- windows7 下安装使用memcached
Memcached 安装使用 本地环境:Windows7 64位web环境:wamp集成环境,php版本:PHP Version 7.1.17 学习参考网站: RUNOOB.COM官网 http:/ ...
- .htaccess重写、安全防护、文件访问权限
今天在<外刊IT评论>上看见了关于.htaccess的使用总结,觉得很不错的,因为wp博客还有其他的php的web服务站点好多都是用.htaccess来管理比如效率以及安全的问题,有必要来 ...
- 洛谷—— P1785 漂亮的绝杀
https://www.luogu.org/problem/show?pid=1785 题目背景 话说absi2011的企鹅在和斗神塔第60层的Boss战斗 不好,这局要输了,企鹅还剩4血了Boss还 ...