HDU-5056-Boring count-周赛4补题
2015-08-24 08:47
357 查看
题意:
给你一个串s,让你去计算一下有多少符合串中相同字母的个数不大于k值的子串的数目
思路:
尺取法; 设两个指针h,t,让t 向右移动,确保现在的子串满足题目要求,不满足的话,把h向右移动,直到满足为止,当t到串的尾部停止;
CODE
给你一个串s,让你去计算一下有多少符合串中相同字母的个数不大于k值的子串的数目
思路:
尺取法; 设两个指针h,t,让t 向右移动,确保现在的子串满足题目要求,不满足的话,把h向右移动,直到满足为止,当t到串的尾部停止;
CODE
#include<stdio.h> #include<string.h> #include<stdlib.h> #define LL __int64 int a[30]; char s[110000]; int main() { int k,t,len,i,pos,x; LL sum; scanf("%d",&t); while(t--) { sum = 0; scanf("%s",s); scanf("%d",&k); memset(a,0,sizeof(a)); len = strlen(s); pos = 0; for(i = 0; i < len;i++) { x=s[i]-'a'; a[x]++; if(a[x]>k)///如果当前的字母数已经大于k了,就让左边的指针右移,更新该串的字母的次数 { while(s[pos]!=s[i]) { a[s[pos]-'a']--;///不是子串中的字母要把对应的次数减去1 pos++; } a[s[pos]-'a']--;///前移一位,让对应字母数小于等于k; pos++; } sum+=i-pos+1;///增加子串数目 } printf("%I64d\n",sum); } return 0; }
相关文章推荐
- Unity换装之分离元素
- eclipse中新建jsp页面出错怎么解决
- 黑马程序员-----Java基础-----IO流-2
- Python 短见
- org.apache.taglibs.standard.tlv.JstlBaseTLV.validate
- 在导航视图控制器加多个标题
- Set、Map、TreeSet、TreeMap操作代码实战之Scala学习笔记-31
- Install Nginx on Ubuntu 14.04 LTS
- hdu 5406 2015 多校联合训练赛#10 dp
- ACM-ICPC如何起步[转]
- js中(function(){…})()立即执行函数写法理解
- 比较好的最长公共子序列的变形
- 5)Win10-UWA开发 XAML 语言等(必须掌握)
- 使用jquery点击一个实现button或连接,进行以下div显示,在点击隐藏
- unity中 拖拽任意的对象
- 编写高质量代码改善C#程序的157个建议——建议152:最少,甚至是不要注释
- 周期-14. 最大公约数与最小公倍数(15)
- 初识MVC
- 初识MVC
- POJ2778 DNA Sequence AC自动机+快速幂+DP