hdu 5672 String(BC——查找子串的个数 模拟)
来源:互联网 发布:智取数字3软件 注册码 编辑:程序博客网 时间:2024/06/05 00:10
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5672
String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1133 Accepted Submission(s): 367
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 leastk(1≤k≤26) distinct characters?
How many substrings there are that contain at least
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 stringS .
The second line contains a integerk(1≤k≤26) .
The first line contains string
The second line contains a integer
Output
For each test case, output the number of substrings that contain at least k dictinct characters.
Sample Input
2abcabcabca4abcabcabcabc3
Sample Output
055
Source
BestCoder Round #81 (div.2)
有一个 10≤长度≤1,000,000 的字符串,仅由小写字母构成。求有多少个子串,包含有至少k(1≤k≤26)个不同的字母?
解题思路:采用两个指针,轮流更新左右边界,同时累加答案。注意:1、在更换头指针的时候,需要考虑是否我用来记录的num值可以减掉,因为如果两个字符相同的话,在这个子串中不同字符的数目是不变的。
2、如果每更换一次头指针,我就把i重新再来一次,就会超时。
详见代码。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define ll long longconst int N=1e6+9;char ch[N];int k;int vis[300];int main(){ int t; scanf("%d",&t); while (t--) { scanf("%s%d",ch,&k); int len=strlen(ch); int head=0,num=0; ll ans=0; memset(vis,0,sizeof(vis));//vis是用来表示字符出现的次数,而不是标记是否出现过 for (int i=0; i<len; i++) { if (vis[ch[i]]==0)//查找一段区间内不同字符的数量 num++; vis[ch[i]]++; while (num>=k)//只要和至少的k相同时,就可以直接的计算出后面的有多少个 { ans+=len-i; if (vis[ch[head]]==1)//判断这个字符是不是只出现一次,如果是的话num--,否则num不变 num--; vis[ch[head]]--; head++; //memset(vis,0,sizeof(vis)); } } printf ("%lld\n",ans); } return 0;}
0 0
- hdu 5672 String(BC——查找子串的个数 模拟)
- HDU 5672 查找子串的个数 思维 双指针
- hdoj String 5672 (字符串模拟)求至少有k个不重复的字符的子串个数
- 查找子串的个数
- (1)查找子字符串的个数
- HDU 6194 String String String 后缀数组 正好出现K次的子串个数 CSU1632 至少出现2次的子串个数
- hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)
- 华为机试—查找子串个数
- 2017沈阳网络赛 1001 HDU 6194 string string string(后缀自动机 出现k次的子串个数)
- 查找子字符串的个数(二分法查找)
- string 查找子串
- hdu 1544(求回文子串的个数)
- hdu 1544 回文子串的个数
- string查找子串的方法
- hdu 5365 Run(BC 50 B题)(求四边形的个数)
- 查找字符串中一个子串的个数
- HDU 5672:String【模拟】
- 华为机试——查找子字符串个数
- kylin 安装配置实验
- 亲测,安装cocoa pods,简单,暴力,有效!
- 探秘Java中String、StringBuilder以及StringBuffer
- HashMap、LinkedHashMap元素遍历机制探讨
- Guava的自动刷新使用
- hdu 5672 String(BC——查找子串的个数 模拟)
- 聊聊 Linux 中的五种 IO 模型
- ThinkPHP关联模型基本使用教程
- NYOJ 1112 求次数
- HDU 4293 Groups【区间dp】
- JS 右键菜单
- Android 实现匿名内部类监听
- Exynos4412如何实现DVFS(动态电压频率调整)
- commons-fileupload 多文件上传