zoukankan      html  css  js  c++  java
  • HDU5056 BoringCount--线性扫一遍

    11754936 2014-09-29 10:08:45 Accepted 5056 31MS 392K 1257 B G++ czy

    好简单的思路,怎么就没想到呢。。。。。

    Boring count

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 250    Accepted Submission(s): 98

    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
     
    Recommend
    heyang   |   We have carefully selected several similar problems for you:  5057 5055 5054 5053 5052 

    官方题解:

    1003 Boring count
    枚举字符串下标i,每次计算以i为结尾的符合条件的最长串。那么以i为结尾的符合条件子串个数就是最长串的长度。求和即可。
    计算以i为结尾的符合条件的最长串两种方法:
    1.维护一个起点下标startPos,初始为1。如果当前为i,那么cnt[str[i]]++,如果大于k的话,就while( str[startPos] != str[i+1] ) cnt[str[startPos]]--, startPos++; 每次都保证 startPos~i区间每个字母个数都不超过k个。ans += ( i-startPos+1 )。 时间复杂度O(n)
    2.预处理出所有字母的前缀和。然后通过二分找出以i为结尾的符合条件的最长串的左边界。时间复杂度O(nlogn),写的不够好的可能超时。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<cstdio>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<queue>
     8 #include<map>
     9 #include<string>
    10 
    11 #define N 100005
    12 #define M 15
    13 #define mod 10000007
    14 //#define p 10000007
    15 #define mod2 100000000
    16 #define ll long long
    17 #define LL long long
    18 #define maxi(a,b) (a)>(b)? (a) : (b)
    19 #define mini(a,b) (a)<(b)? (a) : (b)
    20 
    21 using namespace std;
    22 
    23 int T;
    24 int n;
    25 ll l;
    26 char s[N];
    27 ll ans;
    28 ll vis[30];
    29 ll k;
    30 ll te;
    31 
    32 void ini()
    33 {
    34     ans=0;
    35     scanf("%s",s);
    36     scanf("%I64d",&k);
    37     l=strlen(s);
    38     memset(vis,0,sizeof(vis));
    39 }
    40 
    41 
    42 void solve()
    43 {
    44     ll i;
    45     ll pre;
    46     pre=0;
    47     for(i=0;i<l;i++){
    48         vis[ s[i]-'a' ]++;
    49         while(vis[ s[i]-'a' ]>k){
    50             vis[ s[pre]-'a' ]--;
    51             pre++;
    52         }
    53         ans+=i-pre+1;
    54     }
    55 }
    56 
    57 void out()
    58 {
    59     printf("%I64d
    ",ans);
    60 }
    61 
    62 int main()
    63 {
    64     //freopen("data.in","r",stdin);
    65     //freopen("data.out","w",stdout);
    66     scanf("%d",&T);
    67    // for(int ccnt=1;ccnt<=T;ccnt++)
    68     while(T--)
    69    // while(scanf("%d",&n)!=EOF)
    70     {
    71       //  if(n==0 && m==0) break;
    72         //printf("Case %d: ",ccnt);
    73         ini();
    74         solve();
    75         out();
    76     }
    77 
    78     return 0;
    79 }
  • 相关阅读:
    安卓7.0手机拍照闪退问题解决
    自定义字体TextView
    Android Studio中的CmakeList NDK配置
    动态规划之最长公共子序列(LCS)
    快速排序
    KMP算法实现
    数据结构中的栈
    双向链式线性表(模板实现)
    Android Studio配置OpenCV(非NDK)
    AndroidStudio 1.4配置NDK
  • 原文地址:https://www.cnblogs.com/njczy2010/p/3999620.html
Copyright © 2011-2022 走看看