HDU 5672 String【尺取法】
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5672
题意:
有一个10≤长度≤1,000,000的字符串,仅由小写字母构成。求有多少个子串,包含有至少k(1≤k≤26)个不同的字母?
分析:
很典型的尺取法。
不断依次移动区间的头尾,使区间满足条件,并找到这样的区间个数。
注意说的是包含至少k个,所以只要找到正好包含k个的区间,然后加上包含后面的串的个数就好了。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1e6 + 5;
char s[maxn];
int c[26 + 5];
int main (void)
{
int T;scanf("%d", &T);
while(T--){
scanf("%s", s);
int k;scanf("%d", &k);
int len = strlen(s);
int l = 0, r = 0;
int cnt = 0;
long long ans = 0;
memset(c, 0, sizeof(c));
while(l <= r && l < len){
while(cnt < k && r <len){
c[s[r] -'a']++;
if(c[s[r]-'a'] == 1) cnt++;
r++;
}
if(cnt == k) ans += len - r + 1;
if(c[s[l]-'a'] == 1) cnt--;
c[s[l]-'a']--;
l++;
}
printf("%I64d\n", ans);
}
}
HDU 5672 String【尺取法】的更多相关文章
- hdu 5672 String 尺取法
String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- HDU 5672 String 尺取法追赶法
String Problem Description There is a string S.S only contain lower case English character.(10≤lengt ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- HDU 5672 String 【尺取】
<题目链接> 题目大意:给定一个只由26个小写字母组成的字符串,现在问你至少包含k个不同字母的连续子序列总数有多少. 解题分析:经仔细研究,我们发现,每次尺取到符合要求的最小区间,然后将区 ...
- HDU 5672 String
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5672 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- HDU 4123 (2011 Asia FZU contest)(树形DP + 维护最长子序列)(bfs + 尺取法)
题意:告诉一张带权图,不存在环,存下每个点能够到的最大的距离,就是一个长度为n的序列,然后求出最大值-最小值不大于Q的最长子序列的长度. 做法1:两步,第一步是根据图计算出这个序列,大姐头用了树形DP ...
- hdu 5510 Bazinga KMP+尺取法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:至多50组数据,每组数据至多500个字符串,每个字符串的长度最长为2000.问最大的下标( ...
- Vasya and String(尺取法)
Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- HDU 6103 Kirinriki(尺取法)
http://acm.hdu.edu.cn/showproblem.php?pid=6103 题意: 给出一个字符串,在其中找两串互不重叠的子串,计算它们之间的dis值,要求dis值小于等于m,求能选 ...
随机推荐
- Stream的去重排序
1.List<Integer>排序 List<Integer> list = new ArrayList<>();list.add(50);list.add(25) ...
- div 无缝滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org ...
- GIL(全局解释器锁) 理解
GIL 锁,全局解释器锁,作用就是,限制多线程同时执行,保证同一时间内只有一个线程在执行. 线程非独立的,所以同一进程里线程是数据共享,当各个线程访问数据资源时会出现竞状态,即数据可能会同时被多个 ...
- How to class-dump iPad apps?
http://stackoverflow.com/questions/4776593/how-to-class-dump-ipad-apps The issue here is that the bi ...
- 外观模式(Facade)(门面模式、子系统容易使用)
外观(Facade)模式的定义:是一种通过为多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式.该模式对外有一个统一接口,外部应用程序不用关心内部子系统的具体的细节,这样会大大降低 ...
- excel一些常用的函数
函数分类: 关联匹配类 清洗处理类 逻辑运算类 计算统计类 时间序列类 一.关联匹配类 经常性的,需要的数据不在同一个excel表或同一个excel表不同sheet中,数据太多,copy麻烦也不准确, ...
- 【JZOJ5060】【GDOI2017第二轮模拟day1】公路建设 线段树+最小生成树
题面 在Byteland一共有n 个城市,编号依次为1 到n,它们之间计划修建m条双向道路,其中修建第i 条道路的费用为ci. Byteasar作为Byteland 公路建设项目的总工程师,他决定选定 ...
- fedora安装mod_python
3.1 Installing mod_python To install mod_python, we simply run: yum install mod_python 3.2 Configuri ...
- python 对位运算
- Haskell 学习
truncate pi -- 表示截断, 此处结果为 3 haskell中的touple是可变的,而python中是不可变的 lines函数: lines :: String -> [Strin ...