HDOJ 5672 String
来源:互联网 发布:手机淘宝申诉入口 编辑:程序博客网 时间:2024/06/08 15:01
String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 965 Accepted Submission(s): 313
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 integerT(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 leastk dictinct characters.
Sample Input
2abcabcabca4abcabcabcabc3
Sample Output
055
中文题面: BC Round #81 (div.2)1003
题解:额,组合数学和模拟被学弟吊打(;′⌒`) /(ㄒoㄒ)/~~ 想多了,还以为这一题要DP计数,感觉不会搞。 学弟直接模拟计数。我这组合数学太渣渣。。。
按顺序遍历字符串,记录出现的不同字母的个数cnt,和每种字母出现的数量。 当cnt==k时,以当前字符串为子串的子串共有len-i个。然后将最左边的减去,依次记录下去。
代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char s[1000010];int num[30];int main(){int k,t;scanf("%d",&t);while(t--){scanf("%s%d",s,&k);int len=strlen(s);int cnt=0,left=0;long long ans=0;memset(num,0,sizeof(num));for(int i=0;i<len;++i){int temp=s[i]-'a';if(num[temp])num[temp]++;else{num[temp]++;cnt++;}if(cnt==k){ans+=(len-i);while(1){temp=s[left]-'a';num[temp]--;left++;if(!num[temp]){cnt--;break;}elseans+=(len-i);}}}printf("%I64d\n",ans);}return 0;}
0 0
- hdoj 5672 String 【模拟】
- HDOJ 5672 String
- String hdoj 5672(字符串追赶)
- hdoj 5672 String (尺取法)
- hdoj 1894 String Compare 【string】
- hdoj 2847 Binary String
- hdoj 2476 String painter
- 【HDOJ】1708 -> Fibonacci String
- hdoj 1306 String Matching
- HDOJ 4821 String
- HDOJ 3374 String Problem
- HDOJ 5533-Hidden String
- hdoj-1708-Fibonacci String
- hdoj-4357-String change
- HDOJ 1708 Fibonacci String
- hdoj 2476 String painter(dp)
- HDOJ 3398 String -- 组合 数学
- HDOJ--4821--String【字符串hash】
- 旋转动画
- aa
- 关于格式控制 实例:不同基数形式的输入/输出
- OpenCV3.0 HOG+SVM行人检测器
- OOP学习2
- HDOJ 5672 String
- 软件需求工程(摘抄)
- Codewars-Javascript训练手册:Date 对象
- C++作业-4
- 关于格式控制 实例:格式化输出浮点数
- JavaScript
- 微信学习_03_文本消息
- Julia: MFDCCA和MFCCA算法代码
- 替换空格