BestCoder11(Div2) 1003 Boring count (hdu 5056) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056
题目意思:给出一条只有小写字母组成的序列S,问当中可以组成多少条每个字母出现的次数 <= k 的子序列。
常规方法就是两重 for 循环暴力,很不幸的是,容易想到的方法往往会导致——TLE,恭喜~~
所以必须要想出一条特别的方法来处理,将两重循环降到一重!(这个方法我是看题解啦)
有两个指针:startpos 和 i 。含义表示从数组下标 startpos 到 i 中可以组成的最长符合条件的序列的开始点和结束点。其次,要有一个cnt数组,用来统计 startpos 到 i 中 每个出现的次数,如果当前处理的数组下标 i 所表示的字母 > k,startpos 要往后移动,并且cnt[s[startpos]-'a']--。其实出现这种情况无非就是 s[startpos] == s[i] 了,直到cnt[s[i]-'a'] <= k 跳出循环,此时最长序列数为 i - startpos + 1,不断累加就是答案。时间复杂度为O(n),因为整个序列的处理一直是向后处理的,没有回溯。
(代码中的 j 就是 上面说的 i)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; typedef __int64 LL;
const int N = 1e5 + ;
const int maxn = + ; char s[N];
int cnt[maxn]; int main()
{
int T, k;
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%d", &T) != EOF)
{
while (T--)
{
scanf("%s", s);
scanf("%d", &k);
memset(cnt, , sizeof(cnt));
int len = strlen(s);
int j = , startpos = ;
LL ans = ; for (int j = ; j < len; j++)
{
cnt[s[j]-'a']++;
while (cnt[s[j]-'a'] > k)
{
cnt[s[startpos]-'a']--;
startpos++;
}
ans += j - startpos + ;
}
printf("%I64d\n", ans);
}
}
return ;
}
BestCoder11(Div2) 1003 Boring count (hdu 5056) 解题报告的更多相关文章
- BestCoder9 1003 Revenge of kNN(hdu 4995) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4995 题目意思:在一个一维坐标轴上,给出位置 xi 和值 vi,对于 M 次询问,每次询问给出inde ...
- BestCoder18 1002.Math Problem(hdu 5105) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105 题目意思:给出一个6个实数:a, b, c, d, l, r.通过在[l, r]中取数 x,使得 ...
- BestCoder17 1002.Select(hdu 5101) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有 ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- BestCoder8 1001.Summary(hdu 4989) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989 题目意思:给出 n 个数,然后将这些数两两相加,得到 n*(n-1) /2 对和,把重复的和去掉 ...
- BestCoder24 1001.Sum Sum Sum(hdu 5150) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5150 题目意思:就是直接求素数. 不过 n = 1,也属于答案范围!!只能说,一失足成千古恨啊---- ...
- BestCoder22 1002.NPY and arithmetic progression(hdu 5143) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 题目意思:给出 1, 2, 3, 4 的数量,分别为a1, a2, a3, a4,问是否在每个数 ...
- BestCoder20 1002.lines (hdu 5124) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...
- BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5108 题目意思:给出一个数正整数 N,N <= 1e9,现在需要找出一个最少的正整数 M,使得 ...
随机推荐
- 无线安全: 通过伪AP进行DHCP+DNS劫持的钓鱼攻击
有了之前学习802.11的数据帧格式.芯片硬件参数学习的基础后,我们接下来继续学习无线安全中黑客是怎样进行流量劫持攻击的 相关学习资料 http://www.freebuf.com/articles/ ...
- 新建的 web 工程 有红色的惊叹号
新建的 web 工程 有红色的感叹号问题: 在eclipse 中新建一个web工程,但是工程上有红色的感叹号.解决: 1.右键工程,选择Build Path-->Configur ...
- 多线程下载的原理&断点下载的原理
1)多线程下载说明:
- php中图片文件的导入,上传与下载
---------------------------------------------图片的导入-------------------------------------------------- ...
- hdu 1008 Elevator
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description The hig ...
- 如何使用网盘托管git项目
话说近年来git已经成为项目源代码管理的标准工具,有不少免费托管网站可供使用,详情参考这篇文章: http://www.cnblogs.com/zdz8207/archive/2012/05/20/2 ...
- xss之全面剖析
XSS 全称(Cross Site Scripting) 跨站脚本攻击, 是Web程序中最常见的漏洞.指攻击者在网页中嵌入客户端脚本(例如JavaScript), 当用户浏览此网页时,脚本就会在用户的 ...
- centos命令
alt + z 打开终端(自定义命令) su 切换到root
- Linux创建线程
#include"stdio.h" #include"pthread.h" #include"unistd.h" ; void *creat ...
- linux kill信号列表
linux kill信号列表 $ kill -l1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL5) SIGTRAP 6) ...