hdu 5672 String 尺取法
2016-04-28 20:02
323 查看
String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each test case, output the number of substrings that contain at least k dictinct characters.
[align=left]Sample Input[/align]
2
abcabcabca
4
abcabcabcabc
3
[align=left]Sample Output[/align]
0
55
[align=left]Source[/align]
BestCoder Round #81 (div.2)
思路:就是从左端点找到最近的那个符合条件的右端点;这题特容易超时
尺取法:http://wenku.baidu.com/link?url=_jFXiTHG4ZN60Ki0U5Svb26oKLbbUMtJAwrSnkDC1W1e9RqFK_DaolSUE3MyCKmrv2oGEKWn_GN5P7IQuV0Qp5jxA1SApZHSBYI4NqEYq_u
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> #define true ture #define false flase using namespace std; #define ll __int64 #define inf 0xfffffff int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - '0' ; while( ( ch = getchar() ) >= '0' && ch <= '9' ) res = res * 10 + ( ch - '0' ) ; return res ; } int flag[30]; char a[1000010]; int main() { int x,y,z,i,t; scanf("%d",&x); while(x--) { memset(flag,0,sizeof(flag)); scanf("%s",a); scanf("%d",&y); int st=0,en=0,ji=0; ll ans=0; int len=strlen(a); while(1) { while(en<len&&ji<y) { if(flag[a[en]-'a']==0) ji++; flag[a[en]-'a']++; en++; } if(ji<y)break; ans+=(len-en+1); flag[a[st]-'a']--; if(flag[a[st]-'a']==0) ji--; st++; } printf("%I64d\n",ans); } return 0; }
View Code
相关文章推荐
- 软件改进
- HTTP 常用状态码
- set集合容器
- 《架构漫谈》有感
- 【团队项目】2.0
- 20145304 Java第九周学习报告
- [AngularJS] angular-md-table for Angular material design
- java简单MD5加密
- 第一阶段绩效评估
- Mybatis 一对一,一对多,多对一,多对多的理解
- 如何判断除法运算结果是否为整数
- 解决点击ProgressDialog之外的地方Dialog消失的问题
- Android 主题资源风格定制 <1>
- java String非空判断
- 2011 铺地毯
- Cen 4000 tos7 下的SVN安装与配置
- 关于json
- 字符串hash的用处
- Android 适配不同分辨率屏幕
- 另一篇函数调用栈 相关