HDU 5672 String(尺取法)
2016-04-23 12:28
393 查看
Problem Description
There is a string S.S only
contain lower case English character.(10≤length(S)≤1,000,000)
How many substrings there are that contain at least k(1≤k≤26) distinct
characters?
Input
There are multiple test cases. The first line of input contains an integer T(1≤T≤10) indicating
the number of test cases. For each test case:
The first line contains string S.
The second line contains a integer k(1≤k≤26).
Output
For each test case, output the number of substrings that contain at least k dictinct
characters.
Sample Input
2
abcabcabca
4
abcabcabcabc
3
Sample Output
0
55
Source
BestCoder Round #81 (div.2)
题意:给你一个字符串,问你有多少子串包含k个不同的字母。
思路:如果有一段字符刚刚好满足条件,那么后面的包含这个串的子串全部满足,我们可以尺取l和r,对于一个r满足的话,后面len - r + 1个子串也满足,然后更新l就可以了,因为l和r是分开更新的,所以复杂度是O(n)。
There is a string S.S only
contain lower case English character.(10≤length(S)≤1,000,000)
How many substrings there are that contain at least k(1≤k≤26) distinct
characters?
Input
There are multiple test cases. The first line of input contains an integer T(1≤T≤10) indicating
the number of test cases. For each test case:
The first line contains string S.
The second line contains a integer k(1≤k≤26).
Output
For each test case, output the number of substrings that contain at least k dictinct
characters.
Sample Input
2
abcabcabca
4
abcabcabcabc
3
Sample Output
0
55
Source
BestCoder Round #81 (div.2)
题意:给你一个字符串,问你有多少子串包含k个不同的字母。
思路:如果有一段字符刚刚好满足条件,那么后面的包含这个串的子串全部满足,我们可以尺取l和r,对于一个r满足的话,后面len - r + 1个子串也满足,然后更新l就可以了,因为l和r是分开更新的,所以复杂度是O(n)。
#include<iostream> #include<algorithm> #include<cstdio> #include<queue> #include<map> #include<vector> #include<cstring> #include<cmath> using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; const double pi = acos(-1.0); const int N = 1e6 + 10; char s ; int main() { int t, k, vis[40]; cin>>t; while(t--) { scanf("%s", s); scanf("%d", &k); memset(vis, 0, sizeof(vis)); int l = 0, r = 0, len = strlen(s), temp, num = 0; ll ans = 0; while(l<len) { while(r < len && num < k) { temp = s[r] - 'a'; if(!vis[temp]) num++; vis[temp]++; r++; } if(num<k) break; ans += len - r + 1; temp = s[l] - 'a'; vis[temp]--; if(vis[temp]==0) num--; l++; } cout<<ans<<endl; } return 0; }
相关文章推荐
- Accessing Members of an Enclosing Class
- javascript 传参练手
- 字符串相加,可以吗?
- JS百度分享栏
- Hystrix学习(2)雪崩效应
- 我的问道游戏主题皮肤
- Myeclipse10.x破解方法
- excel批量转换日期格式,将yyyymmdd类型日期转换成yyyy-mm-dd等日期类型方法
- ROS探索总结(十六)(十七)(十八)(十九)——HRMRP机器人的设计 构建完整的机器人应用系统 重读tf 如何配置机器人的导航功能
- Swiper 中文API手册
- Linux GCC常用命令
- 树莓派 centos7 arm版本下载地址
- shell常用语法汇总
- 系统从win7更新到win10没有声音(扬声器一直显示未插入)
- 快速幂取余
- 解决404和there is no action mapped for action的问题
- 【个推推送】如果接受不到推送,如果你确定所有的xml配置都是好的
- ROS探索总结(十三)(十四)(十五)——导航与定位框架 move_base(路径规划) amcl(导航与定位)
- 深度学习入门-4.1 AND.py 源码分析
- explorer.exe不是有效的Win32应用程序