HDU 5056 Boring count(窗口滑动法)

题目地址:HDU 5056

我晕啊。。当时绝壁脑残了。。。当时想到的方法是完全正确的。。但是在脑算第二个样例的时候,居然一直把前三个abc当成了9种。。。于是后面的三个abc每个都要递增2,而不是我想的方法中的递增3。。。于是一直没写代码。。。

言归正传。。

这题的思路就是窗口滑动,两个指针,让指针内的始终保持每个字母的数量少于k个。然后递推过去。然后因为每次右指针往右移动一个的时候,就相当于右指针当前指的数可以与左边的每一个之间都形成一个子序列。所以要增加当前窗口的数量。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define LL __int64
char s[110000];
int a[30];
int main()
{
    int t, k, len, i, pos, x;
    LL sum;
    scanf("%d",&t);
    while(t--)
    {
        sum=0;
        scanf("%s",s);
        scanf("%d",&k);
        memset(a,0,sizeof(a));
        len=strlen(s);
        pos=0;
        for(i=0;i<len;i++)
        {
            x=s[i]-'a';
            a[x]++;
            if(a[x]>k)
            {
                while(s[pos]!=s[i])
                {
                    a[s[pos]-'a']--;
                    pos++;
                }
                a[s[pos]-'a']--;
                pos++;
            }
            sum+=i-pos+1;
            //printf("%d\n",sum);
        }
        printf("%I64d\n",sum);
    }
    return 0;
}
时间: 2024-12-26 11:08:32

HDU 5056 Boring count(窗口滑动法)的相关文章

HDU 5056 Boring count(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056 Problem Description 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 t

hdu 5056 Boring count

http://acm.hdu.edu.cn/showproblem.php?pid=5056 题意:给你一个字符串,然后找出子串中每一个字母出现次数小于等于k的个数. 思路:枚举字符串下标i,每次计算以i为结尾的符合条件的最长串.那么以i为结尾的符合条件子串个数就是最长串的长度.求和即可. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 100010 5 #de

HDU 5056 Boring Count --统计

题解见官方题解,我这里只实现一下,其实官方题解好像有一点问题诶,比如 while( str[startPos] != str[i+1] ) cnt[str[startPos]]--, startPos++; 那个str[i+1]的话会越界.应该是这样: while(str[startPos] != str[i]) 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdl

HDU 5056 Boring count(BestCoder Round #11 (Div. 2))

Problem Description: 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

hdu 5056 Boring count (类似单调队列的做法。。)

给一个由小写字母构成的字符串S,问有多少个子串满足:在这个子串中每个字母的个数都不超过K. 数据范围: 1<=T<= 1001 <= the length of S <= 1000001 <= K <= 100000 思路: 考虑以S[i]结尾的子串,若以S[j] (j<i)作为头不能构成一个符合条件的子串,则S[1]...S[j]都不能作为子串的头. 若S[j+1]可以作为子串的头,则以S[i]结尾的符合条件的子串个数是i-j. 做法:单调队列的思想,不多解释,

HUD 5056 Boring count

Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description You are given a string S consisting of lowercase letters, and your task is counti

(白书训练计划)UVa 11572 Unique Snowflakes(窗口滑动法)

题目地址:UVa 11572 这种方法以前接触过,定义两个指针,不断从左向右滑动,判断指针内的是否符合要求. 这个题为了能快速判断是否有这个数,可以用STL中的set. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ct

hdu Boring count(BestCode round #11)

Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 360    Accepted Submission(s): 140 Problem Description You are given a string S consisting of lowercase letters, and your task is co

Unique Snowflakes(窗口滑动)

题目: Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a machine that captures snowflakes as they fall, and serializes them into a stream of snowflakes that flow, one by one, into a package. Once the pa