String HDU5672
来源:互联网 发布:snmp linux版本查询 编辑:程序博客网 时间:2024/06/01 14:05
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 leastk(1≤k≤26) distinct characters?
How many substrings there are that contain at least
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 stringS .
The second line contains a integerk(1≤k≤26) .
The first line contains string
The second line contains a integer
Output
For each test case, output the number of substrings that contain at least k dictinct characters.
Sample Input
2abcabcabca4abcabcabcabc3
Sample Output
055
大意:
在一个字符串中任意取子串,求其至少有K个不同字母的子串个数。
#include<iostream>#include<cstring>#include<stdio.h>using namespace std;char str[1001000];int num[26],k;int main(){ //freopen("in.txt","r",stdin); int T; cin>>T; while(T--) { long long ans=0; memset(num,0,sizeof(num)); cin>>(str+1)>>k; int cur=0,r=0,l=1,len=strlen(str+1); while(l<=len) { while(r<len&&cur<k) { r++; if(num[str[r]-'a']==0) cur++; num[str[r]-'a']++; } if(cur==k) { ans+=len-r+1; num[str[l]-'a']--; if(num[str[l++]-'a']==0) cur--; while(num[str[l-1]-'a']) { ans+=len-r+1; num[str[l]-'a']--; if(num[str[l++]-'a']==0) cur--; } } else break; } cout<<ans<<endl; }}
大体思路就是两个指针(数组下标)从左到右指,每次都使指中的区域不同字母数为K,加上所有情况。
切记,代码越简明越好。
0 0
- String HDU5672
- hdu5672 String(尺取法)
- hdu5672 string(尺取法)
- HDU5672(尺取法)
- hdu5672尺取法
- HDU5672 尺取
- 几道题(hdu5671 && hdu5672 && hdu5673)
- hdu5672 找字符串的子串问题
- string
- String
- String
- string
- string
- String
- string
- String
- string
- string
- 第六章-数据结构入门
- 【多题合集】网络流24题练习(更新至魔术球问题)
- 【bzoj 1923】[Sdoi2010]外星千足虫(高斯消元)
- 三联莎士比亚阅读摘记
- Arm9+linux fl2440 lcd驱动移植、添加MMC支持、添加U盘支持
- String HDU5672
- dto与po转换类
- 深夜课堂:JavaScript基本概念
- C语言综合练习题(01)
- 高性能网络编程1----accept建立连接
- 画图 201409-2
- 【华为OJ】【算法总篇章】
- 高性能网络编程2----TCP消息的发送
- 算法学习笔记之选择排序算法