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 #define ll long long
 6 using namespace std;
 7
 8 int n,m,t,k;
 9 char str[maxn];
10 int num[maxn];
11
12 int main()
13 {
14     scanf("%d",&t);
15     while(t--)
16     {
17         memset(num,0,sizeof(num));
18         scanf("%s",str);
19         scanf("%d",&k);
20         int k1=strlen(str);
21         int pos=0;
22         ll ans=0;
23         for(int i=0; i<k1; i++)
24         {
25             num[str[i]-‘a‘]++;
26             if(num[str[i]-‘a‘]>k)
27             {
28                 while(str[pos]!=str[i])
29                 {
30                     num[str[pos]-‘a‘]--;
31                     pos++;
32                 }
33                 num[str[pos]-‘a‘]--;
34                 pos++;
35             }
36             ans+=(i-pos+1);
37         }
38         printf("%I64d\n",ans);
39     }
40     return 0;
41 }

时间: 2024-12-23 22:04:37

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

题解见官方题解,我这里只实现一下,其实官方题解好像有一点问题诶,比如 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