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
 
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
 
Recommend
heyang   |   We have carefully selected several similar problems for you:  5057 5055 5054 5053 5052 

官方题解:

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 ;
}

最新文章

  1. POJ1190生日蛋糕[DFS 剪枝]
  2. 配置handler vs2013 iis8.0
  3. spring的PathMatchingResourcePatternResolver-通配符的Resource查找器
  4. 边工作边刷题:70天一遍leetcode: day 85-2
  5. [转]SRTM、ASTER GDEM等全球数字高程数据(DEM)下载方式简介
  6. WPF 操作键盘
  7. [转]Ubuntu 系统 Update-rc.d 命令
  8. freemarker list (长度,遍历,下标,嵌套,排序)
  9. [jumping to the gate] 娱乐向setjmp
  10. HDU-1879-继续畅通工程(并查集)
  11. 【笔记】Kali linux的安装 和 一些使用前的准备工作(原创+转载)
  12. 【转】shell学习笔记(四)——条件测试
  13. 访问vsts私有nuget
  14. MySQL 8.0.11安装配置
  15. Objective-C Collection was mutated while being enumerated crash
  16. 函数和常用模块【day04】:函数的非固定参数(三)
  17. OCM_第二天课程:Section1 &mdash;》配置 Oracle 网络环境
  18. 获取GetOpenFileName多选文件名
  19. H5离线存储-manifest
  20. 20165203 2017-2018-2 《Java程序设计》课程总结

热门文章

  1. UVA 247 - Calling Circles (Floyd)
  2. 从汇编看c++中的多态
  3. shell 复合条件测试 if [ $1 == &quot;1&quot; -o $1 == &quot;0&quot; ] ------==和-eq怎么用
  4. ulrlib案例-爬取百度贴吧
  5. ubuntu frp 自编译。本文不能按顺序来 请自己理解
  6. PHP 腾讯云cos使用之我见
  7. NoSQL 之 Morphia 操作 MongoDB
  8. (19)zabbix Applications使用介绍
  9. CSS3-::selection
  10. Ajax跨域问题---jsonp