hdu5672 string(尺取法)
2016-06-01 21:36
363 查看
String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1332 Accepted Submission(s): 445
Problem Description
There is a string S.S only contain lower case English character.(10≤length(S)≤1,000,000)
How many substrings there are that contain at least k(1≤k≤26) distinct characters?
Input
There are multiple test cases. The first line of input contains an integer T(1≤T≤10) indicating the number of test cases. For each test case:
The first line contains string S.
The second line contains a integer k(1≤k≤26).
Output
For each test case, output the number of substrings that contain at least k dictinct characters.
Sample Input
2
abcabcabca
4
abcabcabcabc
3
Sample Output
0
55
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1332 Accepted Submission(s): 445
Problem Description
There is a string S.S only contain lower case English character.(10≤length(S)≤1,000,000)
How many substrings there are that contain at least k(1≤k≤26) distinct characters?
Input
There are multiple test cases. The first line of input contains an integer T(1≤T≤10) indicating the number of test cases. For each test case:
The first line contains string S.
The second line contains a integer k(1≤k≤26).
Output
For each test case, output the number of substrings that contain at least k dictinct characters.
Sample Input
2
abcabcabca
4
abcabcabcabc
3
Sample Output
0
55
#include<cstdio> #include<cstring> const int maxn=1000005; char a[maxn]; int dig[27]; int main() { int T; scanf("%d",&T); while(T--) { int k; memset(a,0,sizeof(a)); scanf("%s%d",a,&k); int len=strlen(a); long long ans=0; memset(dig,0,sizeof(dig)); int s=0,t=-1,num=0; while(t<len&&s<len) { while(num<k&&(++t)<len) { if(!dig[a[t]-'a'])++num; ++dig[a[t]-'a']; } if(num==k) ans+=(len-t); if(--dig[a[s]-'a']==0)--num; ++s; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- 编译原理之后缀表达式生成与计算(2)
- 定位 -- 百度地图SDK
- Javascript对象定义的几种方式
- 最全面的 Android 编码规范指南
- nrm —— 快速切换 NPM 源
- 钢铁战衣,最靠谱的机械伙伴
- 第九周实践——阅读程序(2)
- 快速原型设计工具 Axure RP 8安装记录
- Java常用排序算法之选择排序
- 【bzoj3160】万径人踪灭 FFT
- windows服务怎么向应用程序发消息(部署在同一台机,非SCOKET)
- 与疯姐的对话
- 第二阶段个人冲刺06
- 个人工作总结09(冲刺二)
- 教师兼干部类
- hive性能优化指南——高级篇
- UVA oj 414 Machined Surfaces(水题)
- scrapy架构设计分析
- Vim编辑器的常用快捷键.
- C#文件监控工具(对追加内容的监控并输出)