【HDU - 5056】Boring count (尺取法)
題干:
You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K.
Input
In the first line there is an integer T , indicates the number of test cases.?
For each case, the first line contains a string which only consist of lowercase letters. The second line contains an integer K.?
[Technical Specification]?
1<=T<= 100?
1 <= the length of S <= 100000?
1 <= K <= 100000
Output
For each case, output a line contains the answer.
Sample Input
3 abc 1 abcabc 1 abcabc 2Sample Output
6 15 21題目大意:
找出字符串中所有字母出現次數不超過k次的子串數量
解題報告:
? ? 尺取就可以了。
AC代碼:
#include<bits/stdc++.h> #define ll long long using namespace std;char a[100000 + 5]; int bk[40]; int k; ll ans; int main() { // freopen("in4.txt","r",stdin);int t,l,r,len;cin>>t;while(t--) {memset(bk,0,sizeof(bk) );ans = 0; scanf("%s",a);scanf("%d",&k);len = strlen(a);l=r=0;while(r<len) {bk[a[r]-'a']++;while(bk[ a[r] - 'a' ] > k) {//注意這是while不是ifbk[ a[l] - 'a']--;l++;} // printf("%lld %d %d \n",ans,l,r);ans+=r-l+1;r++; }printf("%lld\n",ans);} /** //以左端點 while(r<len) {if(l == r) {r++;}}//后續處理 while(l<=r) {} */ return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 5056】Boring count (尺取法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shadowbar.exe - shad
- 下一篇: 今日24时开启!油价或迎年内第2降 重回