HDU5056 BoringCount--线性扫一遍
2024-08-30 14:22:36
11754936 | 2014-09-29 10:08:45 | Accepted | 5056 | 31MS | 392K | 1257 B | G++ | czy |
好简单的思路,怎么就没想到呢。。。。。
Boring count
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 250 Accepted Submission(s): 98
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
[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
abc
1
abcabc
1
abcabc
2
Sample Output
6
15
21
15
21
Source
Recommend
官方题解:
1003 Boring count
枚举字符串下标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<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string> #define N 100005
#define M 15
#define mod 10000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n;
ll l;
char s[N];
ll ans;
ll vis[];
ll k;
ll te; void ini()
{
ans=;
scanf("%s",s);
scanf("%I64d",&k);
l=strlen(s);
memset(vis,,sizeof(vis));
} void solve()
{
ll i;
ll pre;
pre=;
for(i=;i<l;i++){
vis[ s[i]-'a' ]++;
while(vis[ s[i]-'a' ]>k){
vis[ s[pre]-'a' ]--;
pre++;
}
ans+=i-pre+;
}
} void out()
{
printf("%I64d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
while(T--)
// while(scanf("%d",&n)!=EOF)
{
// if(n==0 && m==0) break;
//printf("Case %d: ",ccnt);
ini();
solve();
out();
} return ;
}
最新文章
- POJ1190生日蛋糕[DFS 剪枝]
- 配置handler vs2013 iis8.0
- spring的PathMatchingResourcePatternResolver-通配符的Resource查找器
- 边工作边刷题:70天一遍leetcode: day 85-2
- [转]SRTM、ASTER GDEM等全球数字高程数据(DEM)下载方式简介
- WPF 操作键盘
- [转]Ubuntu 系统 Update-rc.d 命令
- freemarker list (长度,遍历,下标,嵌套,排序)
- [jumping to the gate] 娱乐向setjmp
- HDU-1879-继续畅通工程(并查集)
- 【笔记】Kali linux的安装 和 一些使用前的准备工作(原创+转载)
- 【转】shell学习笔记(四)——条件测试
- 访问vsts私有nuget
- MySQL 8.0.11安装配置
- Objective-C Collection was mutated while being enumerated crash
- 函数和常用模块【day04】:函数的非固定参数(三)
- OCM_第二天课程:Section1 &mdash;》配置 Oracle 网络环境
- 获取GetOpenFileName多选文件名
- H5离线存储-manifest
- 20165203 2017-2018-2 《Java程序设计》课程总结
热门文章
- UVA 247 - Calling Circles (Floyd)
- 从汇编看c++中的多态
- shell 复合条件测试 if [ $1 == ";1"; -o $1 == ";0"; ] ------==和-eq怎么用
- ulrlib案例-爬取百度贴吧
- ubuntu frp 自编译。本文不能按顺序来 请自己理解
- PHP 腾讯云cos使用之我见
- NoSQL 之 Morphia 操作 MongoDB
- (19)zabbix Applications使用介绍
- CSS3-::selection
- Ajax跨域问题---jsonp