http://acm.hdu.edu.cn/showproblem.php?pid=5672

【题意】

给定一个小写英语字母组成的字符串,求这个字符串一共包含多少个至少有m个不同字母的连续子序列

【思路】

尺取。

我们发现,如果i~j是恰好含有k个字母的区间,那么对于k(j<k<=n),i~k是含有至少k个不同字母的子串,那么对于每一个左边界,我们都可以找到一个最小的右边界,使得这个区间恰好含有k个字母,然后统计以这个左边界符合条件的子串个数,找到右边界,用尺取法即可。

【Accepted】

 #include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
char s[maxn];
int n,m;
int vis[];
ll solve()
{
int l=,r=;
ll res=;
memset(vis,,sizeof(vis));
int num=;
while(l<n && r<n)
{
while(r<n && num<m)
{
if(!vis[s[r]-'a'])
{
num++;
}
vis[s[r]-'a']++;
r++;
}
if(num==m)
{
res+=(n-r+);
while(l<=r && vis[s[l]-'a']>)
{
res+=n-r+;
vis[s[l]-'a']--;
l++;
}
vis[s[l]-'a']=;
num--;
l++;
} }
return res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s%d",s,&m);
n=strlen(s);
ll ans=solve();
cout<<ans<<endl;
}
return ;
}

【尺取】HDU String的更多相关文章

  1. HDU 5672 String 【尺取】

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

  2. 【尺取】HDU Problem Killer

    acm.hdu.edu.cn/showproblem.php?pid=5328 [题意] 给定一个长度为n的正整数序列,选出一个连续子序列,这个子序列是等差数列或者等比数列,问这样的连续子序列最长是多 ...

  3. 【单调队列+尺取】HDU 3530 Subsequence

    acm.hdu.edu.cn/showproblem.php?pid=3530 [题意] 给定一个长度为n的序列,问这个序列满足最大值和最小值的差在[m,k]的范围内的最长子区间是多长? [思路] 对 ...

  4. hdu 4123 Bob’s Race 树的直径+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  5. hdu 6231 -- K-th Number(二分+尺取)

    题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...

  6. HDU 5178 pairs【二分】||【尺取】

    <题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...

  7. 【尺取或dp】codeforces C. An impassioned circulation of affection

    http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...

  8. UVA 11019 Matrix Matcher(二维hash + 尺取)题解

    题意:在n*m方格中找有几个x*y矩阵. 思路:二维hash,总体思路和一维差不太多,先把每行hash,变成一维的数组,再对这个一维数组hash变成二维hash.之前还在想怎么快速把一个矩阵的hash ...

  9. POJ3061 Subsequence 尺取or二分

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

随机推荐

  1. nodejs on raspi

    一. https://cnodejs.org/topic/54032efa9769c2e93797cd06 其中的 “安装Node.js” 一节 注意事项: 1. 它用的是v0.10.26(很早的版本 ...

  2. 关于Control.Dispatcher.BeginInvoke卡界面

    Control.Dispatcher.BeginInvoke里的逻辑由UI线程执行,如果内部包含耗时操作就会造成界面卡住. Action.BeginInvoke里的逻辑,将在一个新开的线程中执行,而不 ...

  3. 2018微软实习笔试一道dp题目总结

    题意大概是说在一维数轴上起点和终点的距离是d,现在我们要从起点走到终点.每走一个单位长度消耗一个单位能量,初始时有K单位能量.同时在起点和终点之间分布一些加油站a1,a2,...an,给你加油站数量. ...

  4. 第3章 接口与API设计 52条笔记

    第3章 接口与API设计 52条笔记 第15条: 用前缀避免命名空间冲突 Objective-C 没有其他语言那种内置的命名空间机制 .鉴于此,我们在起名时要设法避免潜在的命名冲突,否则很容易就重名了 ...

  5. Deprecated: Assigning the return value of new by reference is deprecated in报错

    出现了Deprecated: Assigning the return value of new by reference is deprecated in wwwroot\common.inc.ph ...

  6. Uniform Resource Identifier

    https://en.wikipedia.org/wiki/Uniform_Resource_Identifier   "URI" redirects here. For othe ...

  7. HDU 5778 abs (暴力枚举)

    abs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem De ...

  8. CentOS 7上修改主机名

                                       如何在CentOS 7上修改主机名                                           在Cent ...

  9. html5shiv.js让吃屎的IE6、IE7、IE8支持html5去吧

    插件介绍 用于解决IE9以下版本浏览器对HTML5新增标签不识别,并导致CSS不起作用的问题.所以我们在使用过程中,想要让低版本的浏览器,即IE9以下的浏览器支持,那么这款html5shiv.js是一 ...

  10. OSI七层模型和TCP/IP五层模型详解

    OSI是一个开放性的通信系统互连参考模型,他是一个定义得非常好的协议规范.OSI模型有7层结构,每层都可以有几个子层. OSI的7层从上到下分别是 7 应用层 6 表示层 5 会话层 4 传输层 3 ...