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

思路:O(n)的算法,枚举i,求出以每个i结尾的满足条件的串的个数,即以i结尾的最长串的长度。

源代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

#define N 111111
using namespace std;

typedef long long LL;

char str[100001];
int cnt[30];
int k;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>(str+1)>>k;
        memset(cnt,0,sizeof cnt);
        int n=strlen(str+1);
        int pos=0;
        LL res=0;
        for(int i=1;i<=n;i++)
        {
            cnt[str[i]-'a']++;
            if(cnt[str[i]-'a']>k)
            {
                pos++;
                while(str[pos]!=str[i])
                {
                    cnt[str[pos]-'a']--;
                    pos++;
                }
                cnt[str[pos]-'a']--;
            }
            res+=i-pos;
        }
        printf("%I64d\n",res);
    }
    return 0;
}
时间: 2025-01-05 05:35:42

HUD 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(窗口滑动法)

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

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. 做法:单调队列的思想,不多解释,

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----(5056)Boring count(贪心)

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

BestCoder11(Div2) 1003 Boring count (hdu 5056) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056 题目意思:给出一条只有小写字母组成的序列S,问当中可以组成多少条每个字母出现的次数 <= k 的子序列. 常规方法就是两重 for 循环暴力,很不幸的是,容易想到的方法往往会导致——TLE,恭喜-- 所以必须要想出一条特别的方法来处理,将两重循环降到一重!(这个方法我是看题解啦) 有两个指针:startpos 和 i .含义表示从数组下标 startpos 到 i 中可以组成的最长符合条件的序