String


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
 
题意:
 
  有一个 10\leq10≤长度\leq 1,000,000≤1,000,000 的字符串,仅由小写字母构成。求有多少个子串,包含有至少k(1 \leq k \leq 26)k(1≤k≤26)个不同的字母?
 
题解:

  有一个明显的性质:如果子串(i,j)(i,j)包含了至少kk个不同的字符,那么子串(i,k),(j < k < length)(i,k),(j<k<length)也包含了至少kk个不同字符。

  因此对于每一个左边界,只要找到最小的满足条件的右边界,就能在O(1)O(1)时间内统计完所有以这个左边界开始的符合条件的子串。

  寻找这个右边界,是经典的追赶法(尺取法,双指针法)问题。维护两个指针(数组下标),轮流更新左右边界,同时累加答案即可。复杂度 O(length(S))O(length(S))。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N = 1e6+, M = , mod = 1e9 + , inf = 0x3f3f3f3f;
typedef long long ll; int T,k,H[];
char s[N];
int main() {
scanf("%d",&T);
while(T--) {
scanf("%s%d",s+,&k);
int n = strlen(s+);
memset(H,,sizeof(H));
int r = ,cnt = ;
ll ans = ;
for(int l = ;l<=n;l++) {
while(cnt<k&&r<n) {
++r;
if(++H[s[r]]==) cnt++;
}
if(cnt<k) break;
ans = ans+n-r+;
if(--H[s[l]] == ) --cnt;
}
cout<<ans<<endl;
}
return ;
}

HDU 5672 String 尺取法追赶法的更多相关文章

  1. hdu 5672 String 尺取法

    String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  2. HDU 5672 String【尺取法】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5672 题意: 有一个10≤长度≤1,000,000的字符串,仅由小写字母构成.求有多少个子串,包含有 ...

  3. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  4. HDU 5672 String 【尺取】

    <题目链接> 题目大意:给定一个只由26个小写字母组成的字符串,现在问你至少包含k个不同字母的连续子序列总数有多少. 解题分析:经仔细研究,我们发现,每次尺取到符合要求的最小区间,然后将区 ...

  5. HDU 5672 String

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5672 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  6. HDU 4123 (2011 Asia FZU contest)(树形DP + 维护最长子序列)(bfs + 尺取法)

    题意:告诉一张带权图,不存在环,存下每个点能够到的最大的距离,就是一个长度为n的序列,然后求出最大值-最小值不大于Q的最长子序列的长度. 做法1:两步,第一步是根据图计算出这个序列,大姐头用了树形DP ...

  7. hdu 5510 Bazinga KMP+尺取法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:至多50组数据,每组数据至多500个字符串,每个字符串的长度最长为2000.问最大的下标( ...

  8. Vasya and String(尺取法)

    Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  9. HDU 6103 Kirinriki(尺取法)

    http://acm.hdu.edu.cn/showproblem.php?pid=6103 题意: 给出一个字符串,在其中找两串互不重叠的子串,计算它们之间的dis值,要求dis值小于等于m,求能选 ...

随机推荐

  1. NOIP2012 D2 T2 借教室 线段树 OR 二分法

    题目描述: 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自 ...

  2. 从 Zero 到 Hero ,一文掌握 Python--转

    https://www.oschina.net/translate/learning-python-from-zero-to-hero 第一个问题,什么是 Python ?根据 Python 之父 G ...

  3. (C++)错误提示 c2352 :非静态成员函数的非法调用

    静态成员函数相当于全局函数,只是有一个类名字空间的限制.而类成员函数是成员内部的函数,同一个类的对象实例可以有很多,每一个实例都有自已不同的成员变量值,成员函数一般都是对成员自已的成员变量值在操作.所 ...

  4. 关于百分比宽高div居中并垂直居中问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Spring @Scheduled 注解 定时器例子

    <!--xmlns 多加下面的内容--> xmlns:task="http://www.springframework.org/schema/task" <!-- ...

  6. wpf ComboBox 获取选中项的文本内容

    一:根据数据源类型获取选中项 类: public class Region { public int REGION_ID { get; set; } public string REGION_CODE ...

  7. NagiosQL安装

        NagiosQL和Nconf是比较好用的Nagios配置工具. 一.安装NagiosQL 1.在服务器终端运行以下命令: ##YUM安装PHP模块yum -y install --disabl ...

  8. location.reload() 和 location.replace()的区别和应用。

    首先介绍两个方法的语法: reload 方法,该方法强迫浏览器刷新当前页面.语法:location.reload([bForceGet])  参数:bForceGet, 可选参数, 默认为 false ...

  9. 后代children() find()的区别

    $(document).ready(function(){ $("div").children(); });只拿到div下面的子标签元素 $(document).ready(fun ...

  10. Pycharm 设置

    1:显示行号 打上对勾OK 2:设置作者 & 文件编码 3:选择切换Python的版本