Hdu5056 boringcount -- Linear Scan

Source: Internet
Author: User
11754936 10:08:45 Accepted 5056 31 Ms 392 K 1257 B G ++ Czy

I don't think of a simple idea .....

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 descriptionyou 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. inputin 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 outputfor each case, output a line contains the answer. sample input3abc1abcabc1abcabc2 sample output61521 source bestcoder round #11 (div. 2) recommendheyang | we have carefully selected several similar problems for you: 5057 5055 5054 5053

 

Official question:

1003 boring count
Enumerate the string subscript I, and calculate the longest string that matches the condition ending with I each time. The number of matching substrings ending with I is the length of the longest string. Sum.
Two methods are used to calculate the longest string that matches the condition ending with I:
1. Maintain a starting subscript startpos. The initial value is 1. If the current value is I, CNT [STR [I] ++. If the value is greater than K, while (STR [startpos]! = STR [I + 1]) CNT [STR [startpos] --, startpos ++; Ensure that startpos ~ Each letter in the I interval cannot exceed K. Ans + = (I-startpos + 1 ). Time complexity O (N)
2. pre-process the prefixes and values of all letters. Then, the left boundary of the longest string that matches the condition ending with "I" is found through binary. Time complexity O (nlogn), write is not good enough may time out.

 

 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 10000512 #define M 1513 #define mod 1000000714 //#define p 1000000715 #define mod2 10000000016 #define ll long long17 #define LL long long18 #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\n",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 }

 

Hdu5056 boringcount -- Linear Scan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.