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 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
2

Sample Output

6
15
21

Source

BestCoder Round #11 (Div. 2)

官方题解:

代码如下:

#include <cstdio>
#include <cstring>
const int maxn = 100017;
int main()
{
    int t;
    char str[maxn];
    int st[27];
    int k;
    scanf("%d",&t);
    while(t--)
    {
        memset(st,0,sizeof(st));
        scanf("%s",str);
        scanf("%d",&k);
        __int64 ans = 0;
        int len = strlen(str);
        int startpos = 0;
        for(int i = 0; i < len; i++)
        {
            int tt = str[i] -'a';
            st[tt]++;
            if(st[tt] > k)
            {
                while(str[startpos]!=str[i])
                {
                    st[str[startpos]-'a']--;
                    startpos++;
                }
                st[str[startpos]-'a']--;
                startpos++;
            }
            ans+=i-startpos+1;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-08-11 01:34:32

HDU 5056 Boring count(数学)的相关文章

HDU 5056 Boring count(窗口滑动法)

题目地址:HDU 5056 我晕啊..当时绝壁脑残了...当时想到的方法是完全正确的..但是在脑算第二个样例的时候,居然一直把前三个abc当成了9种...于是后面的三个abc每个都要递增2,而不是我想的方法中的递增3...于是一直没写代码... 言归正传.. 这题的思路就是窗口滑动,两个指针,让指针内的始终保持每个字母的数量少于k个.然后递推过去.然后因为每次右指针往右移动一个的时候,就相当于右指针当前指的数可以与左边的每一个之间都形成一个子序列.所以要增加当前窗口的数量. 代码如下: #inc

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

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

HDU 5008 Boring String Problem(西安网络赛B题)

HDU 5008 Boring String Problem 题目链接 思路:构造后缀数组,利用height的数组能预处理出每个字典序开始的前缀和有多少个(其实就是为了去除重复串),然后每次二分查找相应位置,然后在往前往后找一下sa[i]最小的 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int

HDU - 4961 Boring Sum

Problem Description Number theory is interesting, while this problem is boring. Here is the problem. Given an integer sequence a1, a2, -, an, let S(i) = {j|1<=j<i, and aj is a multiple of ai}. If S(i) is not empty, let f(i) be the maximum integer in