hdu 5056 Boring count (窗体滑动)
2017-08-20 12:37
405 查看
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.
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
#include <algorithm>
#include <cstdio>
#include <cstring>
#define LL __int64
#define inf 0x3f3f3f3f
char s[100010];
LL sum[100010];
using namespace std;
int main()
{
LL n,m,cla,i,j,l;
scanf("%I64d",&cla);
while(cla--)
{
memset(sum,0,sizeof(sum));
scanf("%s %I64d",s,&n);
l=strlen(s);
LL pos=0,ans=0;
for(i=0;i<l;i++)
{
sum[s[i]-'a']++;
while(sum[s[i]-'a']>n)//当遇到不符合条件的时候左指针右移,并删除经过的字母的累计次数
{
sum[s[pos]-'a']--;
pos++;
}
ans+=i-pos+1;//当符合条件的时候就加上当前这段的字母个数
}
printf("%I64d\n",ans);
}
return 0;
}
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 2
Sample Output
6 15 21
题目大意:求出一个字符串的子串中反复的小写字母不超过K个的个数。
思路:使用窗体滑动的方法,设置两个指针,分别指在串的左边和右边。当不满足条件时左指针向右移动,直到将当前的右指针删除到符合条件
。然后从原来不符合条件的下一个字母再開始计数。
#include <algorithm>
#include <cstdio>
#include <cstring>
#define LL __int64
#define inf 0x3f3f3f3f
char s[100010];
LL sum[100010];
using namespace std;
int main()
{
LL n,m,cla,i,j,l;
scanf("%I64d",&cla);
while(cla--)
{
memset(sum,0,sizeof(sum));
scanf("%s %I64d",s,&n);
l=strlen(s);
LL pos=0,ans=0;
for(i=0;i<l;i++)
{
sum[s[i]-'a']++;
while(sum[s[i]-'a']>n)//当遇到不符合条件的时候左指针右移,并删除经过的字母的累计次数
{
sum[s[pos]-'a']--;
pos++;
}
ans+=i-pos+1;//当符合条件的时候就加上当前这段的字母个数
}
printf("%I64d\n",ans);
}
return 0;
}
相关文章推荐
- HDU 5056 Boring count(窗口滑动法)
- HDU 5056 Boring count(窗口滑动法或尺缩法)
- hdu 5056 Boring count (窗口滑动)
- HDU-5056-Boring count-周赛4补题
- hdu 5056 Boring count
- HDU 5056 Boring count(数学)
- HDU 5056 Boring count(数学)
- HDU 5056 Boring count
- HDU 5056 Boring count(不超过k个字符的子串个数)
- HDU 5056 - Boring count
- hdu 5056 Boring count(尺取法)
- HDU 5056 Boring count(贪心)
- HDU 5056 Boring count 字符串处统计+滑动窗口法
- hdu 5056 Boring count (类似单调队列的做法。。)
- hdu----(5056)Boring count(贪心)
- HDU 5056 Boring count
- HDU 5056: Boring count 枚举子串?不要急,一个一个来!
- hdu 5056 Boring count ( 窗口转移法)
- BestCoder11(Div2) 1003 Boring count (hdu 5056) 解题报告
- HDU 5056 Boring Count --统计