题目链接:

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【尺取法】的更多相关文章

  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 尺取法追赶法

    String Problem Description There is a string S.S only contain lower case English character.(10≤lengt ...

  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. Ubuntu18.04 + win10双系统下时间问题

    Ubuntu 16.04使用systemd启动之后,时间也改成了由timedatectl来管理 解决的办法可以参考http://blog.sina.com.cn/s/blog_5379c55b0102 ...

  2. php学习知识点框架

    图片来源于知乎,感觉挺全面,通过查看可以更好的了解自己的薄弱知识点,大家共勉.

  3. WPF 单个模块换肤

    xmal: <Window x:Class="wpfSkin.MainWindow" xmlns="http://schemas.microsoft.com/win ...

  4. jnhs-SpringMVC jsp页面向controller传递参数的五种方式

    一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public  String login (String username,String password)   : 解 ...

  5. Laravel 安装mysql、表增加模拟数据、生成控制器

    参考中文网教程: 安装mysql.表增加模拟数据 http://www.golaravel.com/post/2016-ban-laravel-xi-lie-ru-men-jiao-cheng-yi/ ...

  6. 确定比赛名次 HDU - 1285 (拓扑排序)

     注意点: 输入数据中可能有重复,需要进行处理! #include <stdio.h> #include <iostream> #include <cstring> ...

  7. python第一天 :计算机基础(一)

    1.什么是编程语言 答:人类与计算机交流的介质 2.什么是编程 答:利用编程语言控制计算机解决问题 3.为什么要编程 答:可以控制计算机做事,提高生产生活效率 4.计算机的五大组成部分分别有什么作用? ...

  8. 2019-2-16-WPF-封装-dotnet-remoting-调用其他进程

    title author date CreateTime categories WPF 封装 dotnet remoting 调用其他进程 lindexi 2019-02-16 09:40:26 +0 ...

  9. Leetcode62.Unique Paths不同路径

    一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为" ...

  10. golang之下载安装配置

    1.下载:根据操作系统和计算架构选择合适的安装包,操作系统类型有linux.mac.windows等,计算架构分为32位的386计算架构和64位的amd64计算架构 2.安装:推荐安装到 /usr/l ...