HDU 5672 String
题目链接:
hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5672
bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=692&pid=1002
题解:
对于每一个st(0<=st<len),求最小的ed使得str[st...ed]子串刚好包含k个不同的字母,然后累加起来就行了,由于st,ed都是单调不减的,时间复杂度为O(n+n)=O(n)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=; char str[maxn];
int cnt[];
int k; void init(){
memset(cnt,,sizeof(cnt));
} int main(){
int tc;
scanf("%d",&tc);
while(tc--){
init();
scanf("%s",str);
scanf("%d",&k);
int len=strlen(str);
LL ans=;
int sum=;
int ed,st;
for(ed=-,st=;ed<len;){
if(sum>=k){
ans+=len-ed;
cnt[str[st]-'a']--;
if(cnt[str[st]-'a']==) sum--;
st++;
}else{
ed++;
if(cnt[str[ed]-'a']==) sum++;
cnt[str[ed]-'a']++;
}
}
printf("%lld\n",ans);
}
return ;
}
HDU 5672 String的更多相关文章
- HDU 5672 String【尺取法】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5672 题意: 有一个10≤长度≤1,000,000的字符串,仅由小写字母构成.求有多少个子串,包含有 ...
- 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 ...
- HDU 5672 String 【尺取】
<题目链接> 题目大意:给定一个只由26个小写字母组成的字符串,现在问你至少包含k个不同字母的连续子序列总数有多少. 解题分析:经仔细研究,我们发现,每次尺取到符合要求的最小区间,然后将区 ...
- String HDU 5672(双指针)
String HDU 5672(双指针) 传送门 题意:一个字符串中找到所有拥有不少于k个不同的字符的子串. import java.io.*; import java.util.*; public ...
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
- HDU 3374 String Problem(KMP+最大/最小表示)
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 5772 String problem 最大权闭合子图
String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...
- HDU 4821 String(2013长春现场赛I题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...
随机推荐
- QEMU 模拟运行 VxWorks 6.6
QEMU 模拟运行 VxWorks 6.6 项目简介 本项目是在 Windows 系统编译运行 X86 平台 VxWorks 6.6 系统,使用的模拟软件是 qemu for Windows Host ...
- Linux--ps及top、ls命令
day8 ps系统管理命令 ps是强大的后台进程检测命令 格式:ps [options] [--help] 选项参数: 1.-a :显示所有进程,包括PID等,包括其他用户运行的程序 2.-ef:显 ...
- win下python环境搭建以及安装pip、django
1. 安装python并配置 下载安装python,这里我下载的是python2.7,听说2.7比较好用 地址:https://www.python.org/downloads/source/ 记住你 ...
- JAVA乐观锁实现-CAS
是什么 全称compare and swap,一个CPU原子指令,在硬件层面实现的机制,体现了乐观锁的思想. JVM用C语言封装了汇编调用.Java的基础库中有很多类就是基于JNI调用C接口实现了多线 ...
- 后台运行spark-submit命令的方法
在使用spark-submit运行工程jar包时常常会出现一下两个问题: 1.在程序中手打的log(如System.out.println(“***testRdd.count=”+testRdd.co ...
- R语言学习笔记—组合数
组合数:从m个不同元素中取出n(n≤m)个元素的所有组合的个数,叫做从m个不同元素中取出n个元素的组合数. 代码: str_comb <- function(vector){ n <- l ...
- Redis数据结构总结
Redis 字符串(String) SET runoobkey redis GET runoobkey Redis 哈希(Hash) Redis hash 是一个string类型的field和valu ...
- shiro实战整合
引入依赖(包括缓存等): <!-- SECURITY begin --> <dependency> <groupId>org.apache.shiro</gr ...
- SPOJ11469 SUBSET
题面 Farmer John's owns N cows (2 <= N <= 20), where cow i produces M(i) units of milk each day ...
- Mac Eclipse快捷键
Command + O:显示大纲Command + 1:快速修复Command + D:删除当前行Command + Option + ↓:复制当前行到下一行Command + Option + ↑: ...