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,求能选 ...
随机推荐
- PHP1.6--数组
一.数组的键值操作函数 1.array_values() 函数作用是返回数组中所有元素的值,只有一个参数,规定传人给定数组,返回一个包含给定数组中所有值的数组,但不保留键名 被返回的数组将使用顺序的数 ...
- LUOGU P1779 魔鬼杀手_NOI导刊2010提高(03)
传送门 解题思路 背包,首先先用aoe都打残然后单伤补刀,用f[i]表示AOE打了i的伤害的最小花费,g[i]表示单伤打了i的伤害的最小花费. 代码 #include<iostream> ...
- 使用koa-body中间件后DELETE请求中ctx.request.body内容为空
gitbook浏览此随笔 出现场景 在使用koa-body 做文件上传的时候,发现使用DELETE请求时,request.body中的内容为空对象{} app.js //code... const K ...
- Sublime svn 安装
Ctrl + Shift + P 进入 install package 输入svn windows系统选择:TortoiseSVN (在此之前,请确保电脑上已经安装好了TortoiseSVN) li ...
- iview中table的render()函数
Vue 推荐在绝大多数情况下使用 template 来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力,这就是 render 函数,它比 template 更接 ...
- python 实现发送短信验证码
[说明]短信接口使用的是“聚合数据”上面的接口. 那么在使用接口前,需要在聚合数据上面注册,进行申请接口.当然在正式使用之前,我们可以使用申请免得的进行测试. 一.申请成功后,需做的准备工作如下: 1 ...
- Hackerrank--String Function Calculation(后缀数组)
题目链接 Jane loves string more than anything. She made a function related to the string some days ago a ...
- IO流2 --- File类的常用方法1 --- 技术搬运工(尚硅谷)
File类的获取功能 @Test public void test2(){ File file1 = new File("hello.txt"); File file2 = new ...
- Python实例 包机制
每一个.py文件称为一个module,module之间可以互相导入.请参看以下例子: # a.py def add_func(a,b): return a+b # b.py from a im ...
- python 轴向连接